18

I have running nginx on my server ansel.ms and a node.js app on ansel.ms:46156.

I want to setup nginx so it redirects everything from

ansel.ms/rhythm

to

ansel.ms:46156.


ansel.ms/rhythm/sub/path

should become

ansel.ms:46156/sub/path

This is my file in sites-available:

upstream rhythm {
    server ansel.ms:46156;
}

server {
    listen   80;
    server_name ansel.ms www.ansel.ms;
    access_log /srv/www/ansel.ms/logs/access.log;
    error_log /srv/www/ansel.ms/logs/error.log;

    location / {
        root   /srv/www/ansel.ms/public_html;
        index  index.html index.htm;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /srv/www/ansel.ms/public_html$fastcgi_script_name;
    }

    location /rhythm{
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://rhythm;
        proxy_redirect off;
    }
}

I do not really understand deeply what this does (the proxy_set_header stuff), I only copied & pasted it from several sources.

It doesn't work.

Can you give me a hint what to change so it does what I described above? Thank you!

hcs42
  • 13,376
  • 6
  • 41
  • 36
Anselm Eickhoff
  • 589
  • 2
  • 5
  • 14

1 Answers1

28

I cannot spot the error in your configuration file; I'm an nginx-newbie as well.

But here is my full nginx.conf config file which redirects http://myhost/cabaret/foo/bar to http://myhost:8085/foo/bar:

user www-data;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include     /etc/nginx/mime.types;
    access_log  /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;
    tcp_nodelay        on;

    server {

        listen   *:80; ## listen for ipv4
        access_log  /var/log/nginx/localhost.access.log;
        location /cabaret {
               rewrite /cabaret(.*) $1 break;
               proxy_pass http://127.0.0.1:8085;
               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;
        }
    }
}

It is not perfect, because it will not work with http://myhost/cabaret, only if a slash follows carabet like in http://myhost/cabaret/ or http://myhost/cabaret/foo/bar.

hcs42
  • 13,376
  • 6
  • 41
  • 36
  • 9
    Thanks! You can fix the trailing slash problem with this: `server { ... server_name_in_redirect off; ...}` and you have to add a slash to your location like this: `location /rhythm/ {...` (but not in the rewrite!) – Anselm Eickhoff Feb 07 '11 at 17:49
  • This is kind of an 'aside' but are you also using nginx to do your logging for POST requests. I can log my GET requests just fine, but I'm having trouble logging the body response from POST requests. Just thought you might have some insight. My question is posted at: http://stackoverflow.com/questions/4939382/logging-post-data-from-request-body – Chris Barretto Feb 08 '11 at 22:54
  • I am very pleased to have found this question in my time of need. With extra ponus boints for Anselm Eickhoff. – Kawa Apr 04 '13 at 18:37
  • for me, the "rewrite" command made it work, beneath the "location" directive – csotiriou Aug 27 '19 at 21:58