2

I'm trying to configure Nginx to proxy stuff on a localhost

I want localhost to be proxied to localhost:8080, and localhost/test to be proxied to localhost:3000

Here's my current config file

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # Logging Settings
        ##

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

        ##
        # Gzip Settings
        ##

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

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
 ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;
  ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

       server {

               location / {
                    proxy_pass http://localhost:8080;
                    proxy_redirect     off;
                    proxy_set_header    Host            $host;
                    proxy_set_header    X-Real-IP       $remote_addr;
                    proxy_set_header    X-Forwarded-for $remote_addr;
                    port_in_redirect off; 
                    proxy_connect_timeout 300;
                }
                location /test/ {
                    proxy_pass http://localhost:3030;
                    proxy_redirect     off;
                    proxy_set_header    Host            $host;
                    proxy_set_header    X-Real-IP       $remote_addr;
                    proxy_set_header    X-Forwarded-for $remote_addr;
                    port_in_redirect off;
                    proxy_connect_timeout 300;

                }
        }

}
Shankar Kamble
  • 2,983
  • 6
  • 24
  • 40

1 Answers1

3

Ya I found solution .Its worked for me.

 server {
                location /test {
                  proxy_pass http://localhost:3000;
                }
                location / {
                    proxy_set_header    Host            $host;
                    proxy_set_header    X-Real-IP       $remote_addr;
                    proxy_set_header    X-Forwarded-for $remote_addr;
                    proxy_connect_timeout 300;
                    port_in_redirect off;
                    proxy_pass http://localhost:8080;

                }
         }
Shankar Kamble
  • 2,983
  • 6
  • 24
  • 40
  • 1
    Is it because `/test` should be put before `/`, otherwise the situation `/test`, which is included in the situation `/`, will be redirected according to the first rule? – Yan Yang Dec 02 '15 at 07:18