nginx作为Websocket发送短信的代理

时间:2014-08-08 14:07:36

标签: java nginx websocket

我在vm(远程地址端口8090)上运行了websocket服务。使用Nginx代理连接。 nginx配置如下:

server {
    listen 80;
    server_name _;

    location / {
        proxy_pass http://127.0.0.1:8090;
        proxy_pass_request_headers on;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
}

从本地主机我可以使用ip作为ws://111.11.1.1/websocket

连接到websocket

但是,当我从本地主机或应用程序向远程websocket websocket.sendTextMessage("Message")发送消息时,我无法点击套接字。假设我的nginx配置有问题..

更新:通过添加

更改了Nginx的配置

http{ server{..location/{...}}}

当我重新启动Nginx服务时,我收到了错误

nginx emerg http directive is not allowed here in /default.conf:1

nginx: configuration file /nginx.conf test failed

任何建议都有帮助!

1 个答案:

答案 0 :(得分:1)

使用以下配置,它可能会有所帮助。

server {

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_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

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

    }

}