使用具有两个位置的proxy_pass配置Nginx

时间:2013-11-20 11:58:59

标签: nginx proxy reverse-proxy

我正在尝试将Nginx配置为在localhost上代理内容

我希望localhost代理到localhost:8080,localhost / test代理到localhost:3000

这是我当前的配置文件

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;

                }
        }

}

1 个答案:

答案 0 :(得分:3)

我找到了解决方案。它对我有用。

 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;

                }
         }