【问题标题】:Configure Nginx with proxy_pass with two locations使用 proxy_pass 配置 Nginx 和两个位置
【发布时间】:2013-11-20 11:58:58
【问题描述】:

我正在尝试将 Nginx 配置为代理本地主机上的内容

我希望 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;

                }
        }

}

【问题讨论】:

    标签: nginx proxy reverse-proxy


    【解决方案1】:

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

     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;
    
                    }
             }
    

    【讨论】:

    • 是不是因为/test应该放在/之前,否则情况/中包含的情况/test会按照第一条规则重定向?跨度>
    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2020-06-09
    • 2017-08-25
    • 1970-01-01
    • 2017-03-29
    相关资源
    最近更新 更多