nginx redirects back to remote_addr IP
I am trying to setup nginx 1.0.12 as a proxy for apache 2.2.15, but faced a strange problem. And I cannot solve it for third day in a row. I have test site 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. So it seems that nginx tries to download static files from the remote_addr. But if you will add trailing slash to uri like roundcube/ it will work.
Another example: try to open http://www.contra.lv - you also will get redirect back to your own IP address and again nginx tries to download static files from the remote_addr.
Interesting thing is that connencting through apache works nice:
c-craft.info:9091/roundcube
contra.lv:9091
I really need your advice how to fix it, cause I am stuck... Here is my current config files:
nginx.conf
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
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
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)$ {
expires 30d;
root /home/c-craft/public_html;
}
}
sites-enabled/contra.lv.conf
server {
listen 8*.***.**.**6:80;
server_name www.contra.lv contra.lv;
access_log /var/log/nginx/contra.lv_nginx_access_log;
error_log /var/log/nginx/contra.lv_nginx_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)$ {
expires 30d;
root /home/contra/public_html;
}
}
proxy.conf
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:
Listen 9091
UseCanonicalName Off
<VirtualHost 8*.***.**.**6:9091>
Thank you in advance for any advice you have.
Solution 1:
Your proxy_pass
directives should all have trailing slashes, for example, from your default configuration (amended);
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;
}
}
If you update all of your proxy_pass
statements as above to include the trailing slash, you should find that you no longer require the trailing slash in your requested URL, and as such you should no longer see the erroneous returns to your own IP address as you describe.