简体   繁体   English

用于本地webapp和websocket的Nginx设置

[英]Nginx setup for local webapp and websocket

following is my nginx configuration, 以下是我的nginx配置,

server { //PART-1   
        listen 80;
        server_name _;
        location / {
                proxy_pass http://127.0.0.1:8090;
                proxy_redirect off;
                proxy_pass_request_headers on;

                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;

                proxy_http_version 1.1;
                proxy_set_header Upgrade  $http_upgrade;
                proxy_set_header Connection Upgrade;
            }
        }

server { //PART-2
        listen 80;
        server_name service;
        root /usr/local/tomcat7/webapps/service-snapshot;

        location / {
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:8080/ServiceUI/;
                }
        }

first part of config works fine for websockets, which I am already using. config的第一部分对我已经在使用的websockets正常工作。 Second part of config is for webapp running on Apache tomcat 7.0.56, which is not working. config的第二部分是针对在Apache tomcat 7.0.56上运行的webapp,该服务器无法正常工作。

Is there something wrong with config? 配置有问题吗? assuming server_name in both parts might be causing issue! 假设这两部分中的server_name可能引起问题! Any suggestions! 有什么建议么!

While having multiple services on one IP and port is working perfectly fine, the server_name directive is using the HOST header submitted by the client/browser. 虽然在一个IP和端口上具有多种服务可以正常工作,但是server_name指令使用的是客户端/浏览器提交的HOST标头。 In this case, you're not supplying the header but instead asking for a specific location on the same server (you're not asking for http://_ or http://service but for http://yourserver/services from what I see in the comments). 在这种情况下,您不是在提供标题,而是在同一服务器上询问特定的位置(不是从http://_http://service而是从http://yourserver/services我在评论中看到的内容)。

To make it work, you have to specify the different services via location s like this: 要使其正常工作,您必须通过location指定不同的服务,如下所示:

server { 
    listen 80;
    server_name THIS_IS_WHERE_YOUR_DOMAIN_OR_MAYBE_LOCALHOST_GOES;
    location / {
            proxy_pass http://127.0.0.1:8090;
            proxy_redirect off;
            proxy_pass_request_headers on;

            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;

            proxy_http_version 1.1;
            proxy_set_header Upgrade  $http_upgrade;
            proxy_set_header Connection Upgrade;
    }


    location /Service {

            root /usr/local/tomcat7/webapps/service-snapshot;

            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8080/ServiceUI/;
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM