配置Nginx以与Vaadin和WSS一起使用。

3

我正在尝试配置nginx代理wss,以便使用tomcat作为服务器支持vaadin。

以下是我的当前配置:

location / {
        #try_files $uri $uri/ =404;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        proxy_pass http://tomcat/mycontext/;
        proxy_read_timeout 300;

        # wss
        proxy_set_header Connection $http_connection;
        proxy_set_header Connection "upgrade";
}

location /mycontext/ {
        #try_files $uri $uri/ =404;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        proxy_pass http://tomcat/mycontext/;
        proxy_read_timeout 300;

        # wss
        proxy_set_header Connection $http_connection;
        proxy_set_header Connection "upgrade";
}

上游

upstream tomcat {
    server localhost:8080 fail_timeout=0;
}

nginx启动时没有任何投诉,并且在# wss注释后没有这两行代码的情况下可以用于HTTP。

当我尝试连接vaadin时,出现以下错误:

Error in WebSocket connection to wss://myapp.dev/ui/?v-r=push&debug_window

如果有所不同的话,我们正在使用Atmosphere进行wss。
1个回答

3

问题在于代理头信息有误:

我从某个网站上复制了以下内容:

       proxy_set_header Connection $http_connection;
       proxy_set_header Connection "upgrade";

应该是这样的:

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";

请注意,第一行的Connection变成了Upgrade。
因此完整的配置如下。
location / {
        #try_files $uri $uri/ =404;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        proxy_pass http://tomcat/mycontext/;
        proxy_read_timeout 300;

        return 301 $scheme://mysite.dev/mycontext/ui$request_uri;

        # wss
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
}

location /mycontext/ {
        #try_files $uri $uri/ =404;

        #
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        proxy_pass http://tomcat/mycontext/;
        proxy_read_timeout 300;

        # wss
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";

为了完整起见,这里是Vaadin servlet。

package dev.myapp.servlets;

import javax.servlet.annotation.WebServlet;

import com.vaadin.flow.server.ServiceException;
import com.vaadin.flow.server.SessionInitEvent;
import com.vaadin.flow.server.SessionInitListener;
import com.vaadin.flow.server.VaadinServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;

@WebServlet(urlPatterns =
{ "/ui/*"}, name = "MyApp", asyncSupported = true,  loadOnStartup = 1, initParams =
{
        @WebInitParam(name = "org.atmosphere.cpr.AtmosphereInterceptor", value = "dev.myapp.servlets.AtmosphereFilter"),
        @WebInitParam(name = "closeIdleSessions", value = "true"),

        /// changed this when we release.
        @WebInitParam(name = "productionMode", value = "false")

})
public class Servlet extends VaadinServlet implements SessionInitListener
{

    private static final long serialVersionUID = 1L;

    @Override
    protected void servletInitialized() throws ServletException 
    {
        super.servletInitialized();
        getService().addSessionInitListener(this);
    }

    @Override
    public void sessionInit(SessionInitEvent event) throws ServiceException
    {
        event.getSession().setErrorHandler(new CustomExceptionHandler());
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接