我正在尝试将 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 毫无怨言地启动,并且在 #wss 注释后没有两行的情况下为 HTTP 工作。
当我尝试连接到 vaadin 时出现错误:
Error in WebSocket connection to wss://myapp.dev/ui/?v-r=push&debug_window
如果它有所作为,我们正在为 wss 使用气氛。
所以问题是代理标头错误:
我是从一个网站复制的:
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());
}
}