配置nginx以使用vaadin和wss

wko9yo5t  于 2023-04-20  发布在  Nginx
关注(0)|答案(1)|浏览(151)

我正在尝试配置nginx来使用tomcat作为服务器代理vaadin的wss。
以下是我目前的配置:

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启动时没有任何问题,并且可以在HTTP上工作,没有#wss注解后的两行。
当我尝试连接到vaadin时,我得到错误:

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

如果有区别的话,我们将使用atmosphere作为wss。

x759pob2

x759pob21#

所以问题是代理头是错误的:
我从一个网站上复制:

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

相关问题