| Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-23-2012, 08:40 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25
Rep:
|
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 08:42 AM.
|
|
|
|
02-23-2012, 10:50 AM
|
#2
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
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
|
|
|
|
02-23-2012, 01:39 PM
|
#3
|
|
LQ Newbie
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25
Original Poster
Rep:
|
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 02:27 PM.
|
|
|
|
02-24-2012, 03:32 AM
|
#4
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
Quote:
Originally Posted by bzzik
|
Can you access it using the same url, as in nginx: http://8*.***.**.**6:9091/roundcube
You maybe have to check apache vhosts
|
|
|
|
02-24-2012, 01:00 PM
|
#5
|
|
LQ Newbie
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by bathory
|
Yeap, I can. It opens roundcube without a problem.
|
|
|
|
02-25-2012, 05:18 PM
|
#6
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
Hi,
Maybe not what you want, but check if the following works:
Code:
location /roundcube/ {
proxy_pass http://8*.***.**.**6:9091/roundcube/;
}
Regards
|
|
|
|
02-26-2012, 07:15 AM
|
#7
|
|
LQ Newbie
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by bathory
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 08:20 AM.
|
|
|
|
02-26-2012, 11:09 AM
|
#8
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
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.
|
02-26-2012, 01:19 PM
|
#9
|
|
LQ Newbie
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by bathory
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 01:31 PM.
|
|
|
|
02-27-2012, 04:27 AM
|
#10
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
|
|
|
|
02-27-2012, 07:36 AM
|
#11
|
|
LQ Newbie
Registered: Jan 2009
Location: Latvia, Riga
Distribution: CentOS
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by bathory
It works from here.
|
Hmm, without trailing slash?
|
|
|
|
02-27-2012, 08:38 AM
|
#12
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
Quote:
Originally Posted by bzzik
Hmm, without trailing slash?
|
Yup. If it doesn't work for you, clean your browser cache before trying again
Regards
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:15 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|