LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 02-23-2012, 09:40 AM   #1
bzzik
LQ Newbie
 
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25

Rep: Reputation: 0
Strange nginx redirects without trailing slash


Hi ppl

I am trying to setup nginx as a proxy for apache2, but faced strange problem. I have test site http://c-craft.info and have installed roundcube in /roundcube subdirectory. So here is the problem:

If you will try to open http://c-craft.info/roundcube you will get strange redirect back to your own IP address. But if you will add trailing slash to uri http://c-craft.info/roundcube/ it will work.

1. Why does it happen? How to fix it.
2. Should I use regex to force adding trailing slash?

Quote:
rewrite ^(.*[^/])$ $1/ permanent;
Here is my config files:

nginx.conf
Quote:
user nginx;
worker_processes 2;
pid /var/run/nginx.pid;

events {
worker_connections 2048;
use epoll;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

client_max_body_size 64m;
client_body_buffer_size 256k;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;

gzip on;
gzip_vary on;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6].(?!.*SV1)";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
sites-enabled/default
Quote:
server {
listen 80 default;
server_name _;
server_name_in_redirect off;
server_tokens off;
access_log /var/log/nginx/default.access.log;
error_log /var/log/nginx/default.error.log;

location / {
proxy_pass http://8*.***.**.**6:9091;
include /etc/nginx/proxy.conf;
}
}
sites-enabled/c-craft.info.conf
Quote:
server {
listen 80;
server_name www.c-craft.info c-craft.info;
server_name_in_redirect off;

access_log /var/log/nginx/c-craft.info_access_log;
error_log /var/log/nginx/c-craft.info_error_log;

location / {
proxy_pass http://8*.***.**.**6:9091;
include /etc/nginx/proxy.conf;
}

location ~* ^.+\.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ {
expires 30d;
root /home/c-craft/public_html;
}
}
proxy.conf
Quote:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 32 8k;
proxy_busy_buffers_size 64k;
In apache httpd.conf I have:
Quote:
Listen 9091
UseCanonicalName Off
Thank you in advance for any advice you have

Last edited by bzzik; 02-23-2012 at 09:42 AM.
 
Old 02-23-2012, 11:50 AM   #2
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,962

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Hi,

I'm not very familiar with nginx reverse proxy config, but if it works like apache, you have to add a trailing slash at the proxied URL like this:
Code:
proxy_pass http://8*.***.**.**6:9091/ ;
Regards
 
Old 02-23-2012, 02:39 PM   #3
bzzik
LQ Newbie
 
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25

Original Poster
Rep: Reputation: 0
Hi! Nope, it did not help...

Forgot to say, that access through apache works normal http://c-craft.info:9091/roundcube

Last edited by bzzik; 02-23-2012 at 03:27 PM.
 
Old 02-24-2012, 04:32 AM   #4
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,962

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Quote:
Originally Posted by bzzik View Post
Hi! Nope, it did not help...

Forgot to say, that access through apache works normal http://c-craft.info:9091/roundcube
Can you access it using the same url, as in nginx: http://8*.***.**.**6:9091/roundcube
You maybe have to check apache vhosts
 
Old 02-24-2012, 02:00 PM   #5
bzzik
LQ Newbie
 
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
Can you access it using the same url, as in nginx: http://8*.***.**.**6:9091/roundcube
You maybe have to check apache vhosts
Yeap, I can. It opens roundcube without a problem.
 
Old 02-25-2012, 06:18 PM   #6
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,962

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Hi,

Maybe not what you want, but check if the following works:
Code:
location /roundcube/ {
proxy_pass http://8*.***.**.**6:9091/roundcube/;
}
Regards
 
Old 02-26-2012, 08:15 AM   #7
bzzik
LQ Newbie
 
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
Hi,

Maybe not what you want, but check if the following works:
Code:
location /roundcube/ {
proxy_pass http://8*.***.**.**6:9091/roundcube/;
}
Regards
Nope It seems that the problem not only in trailing slash. Heres is another domain I want to setup (Joomla CMS is running on it) http://contra.lv, and it also does not work. Same thing happens - redirects. But on port 9091 works - http://contra.lv:9091.

Update:
Ok, I think I found the problem. For some reason $_SERVER["HTTP_HOST"] is incorrect - it is getting equal to $remote_addr, but not always... this is weird. I am trying to understand how does this happen, is it nginx, apache or PHP problem?

Update 2:
So this sounds like nginx problem... you can check two test addresses: http://contra.lv/server.php and http://contra.lv:9091/server.php. You can see, that HTTP_POST is different on nginx and apache...

Last edited by bzzik; 02-26-2012 at 09:20 AM.
 
Old 02-26-2012, 12:09 PM   #8
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,962

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Quote:
Update 2:
So this sounds like nginx problem... you can check two test addresses: http://contra.lv/server.php and http://contra.lv:9091/server.php. You can see, that HTTP_POST is different on nginx and apache...
Huh! Change $host to $http_host in the header and maybe add an X-Forwarded-Host header in proxy.conf:
Code:
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 32 8k;
proxy_busy_buffers_size 64k;
 
1 members found this post helpful.
Old 02-26-2012, 02:19 PM   #9
bzzik
LQ Newbie
 
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
Huh! Change $host to $http_host in the header and maybe add an X-Forwarded-Host header in proxy.conf:
Code:
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 32 8k;
proxy_busy_buffers_size 64k;
Oh my, you saved my day $_SERVER["HTTP_HOST"] is correct now and contra.lv is working In fact after adding this line
Quote:
proxy_set_header X-Forwarded-Host $http_host;
everything it started to work. But in all tutorials about nginx as reverse_proxy there is nothing about this line...

Something still not right in my server configuration... Interesting thing is, that when I add $host to nginx log_format I see there normal host, but it is not forwarded as $host to apache for some reason.

And the problem with trailing slash still exists: http://c-craft.info/roundcube still redirects back to $remote_addr.

Last edited by bzzik; 02-26-2012 at 02:31 PM.
 
Old 02-27-2012, 05:27 AM   #10
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,962

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Quote:
And the problem with trailing slash still exists: http://c-craft.info/roundcube still redirects back to $remote_addr.
It works from here.
 
Old 02-27-2012, 08:36 AM   #11
bzzik
LQ Newbie
 
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
It works from here.
Hmm, without trailing slash?
 
Old 02-27-2012, 09:38 AM   #12
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,962

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Quote:
Originally Posted by bzzik View Post
Hmm, without trailing slash?
Yup. If it doesn't work for you, clean your browser cache before trying again

Regards
 
  


Reply

Tags
apache, nginx, slash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trailing slash / in path syntax jim_d Linux - Newbie 2 03-24-2011 06:25 PM
Add trailing slash adrianc.grigoras Linux - Software 1 05-13-2009 07:26 AM
Apache trailing slash boondock_saint Linux - General 9 07-07-2004 04:31 PM
Apache trailing slash puzzler tantric Linux - Software 4 04-11-2004 05:18 PM
Necessary trailing slash in apache directories silversky Linux - Software 3 03-03-2004 04:19 AM


All times are GMT -5. The time now is 02:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration