Shankar Kamble
Shankar Kamble

Reputation: 3033

Configure Nginx with proxy_pass with two locations

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;

                }
        }

}

Upvotes: 2

Views: 25291

Answers (2)

Nimeth Nimdinu
Nimeth Nimdinu

Reputation: 31

 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;

                } 

    

according to this your proxxy pass http://localhost:3030/test url location check this

location /test/: This directive matches requests that start with /test/ in the URL path. For example, it would match requests to http://example.com/test/ or http://example.com/test/something.

proxy_pass http://localhost:3030;: This directive specifies that requests matching the location should be forwarded to http://localhost:3030. The URI part of the original request is preserved, so a request to http://example.com/test/foo would be forwarded to http://localhost:3030/test/foo.

proxy_redirect off;: Disables automatic redirection of the Location response header. This is useful when the backend server generates URLs based on its own hostname.

proxy_set_header Host $host;: Sets the Host header to the value of the original request's Host header. This is important for the backend server to know which virtual host the request was intended for.

proxy_set_header X-Real-IP $remote_addr;: Sets the X-Real-IP header to the IP address of the client. This header can be used by the backend server to log the real IP address of the client when behind a proxy.

proxy_set_header X-Forwarded-for $remote_addr;: Sets the X-Forwarded-for header to the IP address of the client. This header is used to track the chain of proxies that a request has passed through.

port_in_redirect off;: Disables including the port number in the Location response header if the backend server responds with a redirect.

proxy_connect_timeout 300;: Sets the timeout for establishing a connection to the backend server to 300 seconds.

Upvotes: 0

Shankar Kamble
Shankar Kamble

Reputation: 3033

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;

                }
         }

Upvotes: 3

Related Questions