LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Strange nginx redirects without trailing slash (https://www.linuxquestions.org/questions/linux-server-73/strange-nginx-redirects-without-trailing-slash-930876/)

bzzik 02-23-2012 08:40 AM

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 :)

bathory 02-23-2012 10:50 AM

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

bzzik 02-23-2012 01:39 PM

Hi! Nope, it did not help...

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

bathory 02-24-2012 03:32 AM

Quote:

Originally Posted by bzzik (Post 4610358)
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

bzzik 02-24-2012 01:00 PM

Quote:

Originally Posted by bathory (Post 4610821)
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.

bathory 02-25-2012 05:18 PM

Hi,

Maybe not what you want, but check if the following works:
Code:

location /roundcube/ {
proxy_pass http://8*.***.**.**6:9091/roundcube/;
}

Regards

bzzik 02-26-2012 07:15 AM

Quote:

Originally Posted by bathory (Post 4612010)
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...

bathory 02-26-2012 11:09 AM

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;


bzzik 02-26-2012 01:19 PM

Quote:

Originally Posted by bathory (Post 4612468)
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 :hattip: $_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. :(

bathory 02-27-2012 04:27 AM

Quote:

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

bzzik 02-27-2012 07:36 AM

Quote:

Originally Posted by bathory (Post 4613022)
It works from here.

Hmm, without trailing slash?

bathory 02-27-2012 08:38 AM

Quote:

Originally Posted by bzzik (Post 4613130)
Hmm, without trailing slash?

Yup. If it doesn't work for you, clean your browser cache before trying again

Regards


All times are GMT -5. The time now is 04:20 AM.