代码之家  ›  专栏  ›  技术社区  ›  user2669989

如何将nginx配置为使用uWSGI在不同子网上运行的不同nginx的反向代理?

  •  0
  • user2669989  · 技术社区  · 7 年前

    这比 this article ,

    下面是当前的流程和配置,它正在工作。我可以从/16机器访问UI。这是别人做的。我根据上面的文章猜测。

    Client------------------nginx----------------------nginx
    192.168.13.90/16   eth0 - 192.168.13.2/16   eth0   - 192.168.13.9/16
                                                eth0.1 - 182.28.129.202/24
                                                         |
                                                         |
                                                       uWSGI
    

    我是nginx和python的初学者。我正在尝试将nginx配置为上的反向代理 eth0.1 在以下场景中,

    Client------------------nginx---------------------nginx
    182.28.129.201/24  eth0 - 192.168.13.9/16   eth0 - 192.168.13.2/16
                       eth0.1 - 182.28.129.202/24
                                                        |
                                                        |
                                                      uWSGI
    

    以下是我当前试用版的当前nginx配置文件;错误注释,

    编辑2- 注释掉特殊位置块 login.html 来自上游nginx反向代理服务器的页面

    # /etc/nginx/nginx.conf
    # 192.168.13.2/16  - this upstream server
    
    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;
    
    events {
        worker_connections 768;
        # multi_accept on;
    }
    
    http {
        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;
    
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
    
        access_log /var/log/access.log;
        error_log /var/log/error.log;
    
        gzip on;
        gzip_disable "msie6";
    
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }
    

    下面是用户界面。上述nginx包含的conf文件。形态, #/etc/nginx/sites-enabled/ui。形态

    # 192.168.13.2/16  -  this upstream server
    
    server {
        listen 192.168.13.2:80;
        server_name 192.168.13.2:80;
    
        access_log /var/log/access.log;
        error_log /var/log/error.log;
    
        # No physical login.html,
        #  it is passing it to root which is login screen
        #location = /login.html {                          <--------- Edit2
        #   return 301 /;
        #}
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/var/run/ui.sock;
        }
    }
    

    以下是nginx。conf开启 eth0.1 在不同的子网上,

    编辑1- 我在这个配置中面临的问题是,请求被代理到上游nginx服务器,但响应是gzip和分块的。我没有看到客户端加载页面。

    编辑2- 我为特殊页面添加了位置块 登录名。html 它加载了页面,但它卡在了重定向页面上。请参见下面的wireshark流,

    # /etc/nginx/nginx.conf
    
    # eth0   - 192.168.13.9/16
    # eth0.1 - 182.28.129.202/24  - Reverse proxy
    
    user www-data;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log debug;
    
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  0;   #Disable
    
        gzip  on;
        gzip_disable    "msie6";                          #   <----------- Edit2
    
        server {
            listen          182.28.129.202:8080;
            server_name     182.28.129.202:8080;
    
            #rewrite        ^(.*) $scheme://$server_name$1 permanent;
    
            location = /login.html {                       #   <---------- Edit2
                proxy_pass              http://192.168.13.2:80/;
            }
    
            location / {
                proxy_pass              http://192.168.13.2:80;
    
                #  trial and error
                #proxy_http_version     1.1;
                #proxy_set_header       Host $host;
                #proxy_set_header       X-Real-IP $remote_addr;
                #proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
                #proxy_set_header       X-Forwarded-Proto http;
                #proxy_bind             192.168.13.9;
                #proxy_buffering        off;
            }
        }
    }
    

    编辑2- Wireshark流,停留在重定向页面,/主页

    POST /login.html HTTP/1.1
    Accept: text/html, application/xhtml+xml, */*
    Referer: http://182.28.129.202:8080/login.html
    Accept-Language: en-US
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    Host: 182.28.129.202:8080
    Content-Length: 33
    DNT: 1
    Connection: Keep-Alive
    Cache-Control: no-cache
    
    password=xxxxxxxxxx&login-submit=Server: nginx/1.6.2
    Date: Thu, 07 Dec 2017 20:10:50 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 239
    Connection: close
    Location: http://192.168.13.2/home
    Set-Cookie: remember_token=admin|c8aa43aab8b27724a207eb28ac7d1034d1e274fc4f528002a2d6106bb7c36a41756f6951d518f632d426a7d8c8257ad00dbab78e3daf7a5bbbc723ba33107e5e; Expires=Fri, 07-Dec-2018 20:10:50 GMT; Path=/
    Set-Cookie: session=.eJw1zksKwzAMRdG9eNyBLUeSnc0E2ZJoBgkln1Hp3msonT4uvPMOix92PsN8Hbc9wrJqmAMgdgOPWhJx06jSM-lEuUKN5CzelFJJJY-pRs5QDLkgRWsdPZZkTZETkRikws1gUufeRL0a9zhlHAlXFEDPYlLHW1RI3sAYwoC87Nhkt_360-7Tjh9PdFv38PkC4lY3Tg.DQsxyg.lZrpSNvXnwE-JHT5t6qlYLAQP4Y; Expires=Sun, 07-Jan-2018 20:10:50 GMT; HttpOnly; Path=/
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>Redirecting...</title>
    <h1>Redirecting...</h1>
    <p>You should be redirected automatically to target URL: <a href="/home">/home</a>.  If not click the link.
    

    首先,我尝试通过HTTP访问UI,然后使用HTTPS。由于我在这方面的知识,我无法继续前进,因此非常感谢任何指导。

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   user2669989    7 年前

    我做了很多尝试和错误,这就是观察奇怪错误的地方。以下是为我工作的配置文件。希望它能帮助一些人。

    # 192.168.13.2/16  -  this upstream server
    
    server {
        listen 192.168.13.2:80;
        server_name 192.168.13.2:80;
    
        access_log /var/log/access.log;
        error_log /var/log/error.log;
    
        # No physical login.html,
        #  it is passing it to root which is login screen
        location = /login.html {
           rewrite ^ http://192.168.13.2/ last;
        }
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/var/run/ui.sock;
        }
    }
    
    # /etc/nginx/nginx.conf
    
    # eth0   - 192.168.13.9/16
    # eth0.1 - 182.28.129.202/24  - Reverse proxy
    
    user www-data;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log debug;
    
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  0;   #Disable
    
        gzip  on;
        gzip_disable    "msie6";
    
        server {
            listen          182.28.129.202:8080;
            server_name     182.28.129.202:8080;
    
            location / {
                proxy_buffering     off;
                proxy_set_header    Host $http_host;
                proxy_set_header    X-Real-IP $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header    X-Forwarded-Proto http;
                proxy_pass          http://192.168.13.2:80;
            }
        }
    }