Asp*_*lis 4 ssl nginx server-sent-events
我制作了一个基于Tomcat和Nginx的web项目.
不得不努力工作,没有错误.
但是,当我将ssl添加到nginx时.停止工作服务器发送事件.
如果我直接使用后端服务器 - 它可以正常工作,所以问题就在于nginx.
有人有这样的问题吗?
这是配置的相关部分
我的nginx.conf(我还没有使用网站启用,也把我的应用配置在这里.在conf结束时的基本设置)./ SecurConfig/api/tutorial/listen - 是事件的来源
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
root /data/;
server{
listen 80;
# server_name ajaxdemo.in.ua;
# proxy_set_header Host ajaxdemo.in.ua;
location / {
rewrite ^(.*)$ https://ajaxdemo.in.ua$1 permanent;
}
}
server {
#listen 80;
listen 443 default ssl;
#ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
root /data/www;
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET';
if ($http_cookie ~* "jsessionid=([^;]+)(?:;|$)") {
set $co "jsessionid=$1";
}
#proxy_set_header Cookie "$co";
proxy_pass http://127.0.0.1:1666/SecurConfig/;
#proxy_pass http://88.81.229.142:1666/SecurConfig/;
add_before_body /header.html;
add_after_body /footer.html;
}
location /SecurConfig/api/tutorial/listen {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET';
##Server sent events set
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffering on;
proxy_buffer_size 8k;
#proxy_cache off;
##
if ($http_cookie ~* "jsessionid=([^;]+)(?:;|$)") {
set $co "jsessionid=$1";
}
#proxy_set_header Cookie "$co";
proxy_pass http://127.0.0.1:1666/;
#proxy_pass http://88.81.229.142:1666/;
}
location /SecurConfig/ {
root /data/www;
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET';
if ($http_cookie ~* "jsessionid=([^;]+)(?:;|$)") {
set $co "jsessionid=$1";
}
#proxy_set_header Cookie "$co";
proxy_pass http://127.0.0.1:1666/;
#proxy_pass http://88.81.229.142:1666/;
add_before_body /header.html;
add_after_body /footer.html;
}
location ~ \.css$ {
root /data/css/;
}
location /header.html {
root /data/www;
}
location /footer.html {
root /data/www;
}
location ~ \.(gif|jpg|png|jpeg)$ {
root /data/images;
}
}
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 100m;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)
nginx的错误日志中没有错误条目.但是还提到了在访问日志中访问/ SecurConfig/api/tutorial/listen.代码200表示"一切正常"
"GET/SecurConfig/api/tutorial/listen HTTP/1.1"200 187" https://ajaxdemo.in.ua/SecurConfig/api/tutorial/map/11111111 ""Mozilla/5.0(Windows NT 6.1; WOW64; rv: 34.0)Gecko/20100101 Firefox/34.0"
Tomcat日志像往常一样显示/ SecurConfig/api/tutorial/listen(例如检查安全访问,接受它,然后重新发送到控制器).
如果我在chrome developer模式下运行我的页面,我会看到此错误
GET https://ajaxdemo.in.ua/SecurConfig/api/tutorial/listen net::ERR_EMPTY_RESPONSE
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)#proxy_buffering on; #proxy_buffer_size 8k; #proxy_cache off;
我还评论了参数
Run Code Online (Sandbox Code Playgroud)#proxy_connect_timeout 300; #proxy_send_timeout 300; #proxy_read_timeout 300;
因此,这些参数返回其默认值(约20秒)并且我的所有数据在~20秒后出现.所以我开始了
Run Code Online (Sandbox Code Playgroud)proxy_connect_timeout 2; proxy_send_timeout 2; proxy_read_timeout 2;
数据显得更快.但它仍然是一个整体(一次3-4个事件),在启用ssl事件之前逐个显示.
仍然是你的帮助和解释,我错了.
为了使SSE正常工作,您必须确保没有任何内容被缓存或缓冲:不在您的脚本中(例如在PHP中我们有@ob_flush();@flush()成语),而不是在您的Web服务器上,而不是在任何中间代理或防火墙上.
你说你注释掉所有与缓冲有关的nginx命令,但注释掉意味着它将使用默认值.例如,对于默认的proxy_buffering是on.我建议明确指定它们以确保关闭所有缓冲和缓存.
proxy_buffering off;
proxy_buffer_size 0;
proxy_cache off;
Run Code Online (Sandbox Code Playgroud)
我还会考虑明确设置超时,而不是评论它们."高"的定义取决于您的应用.例如,如果它总是每隔几秒发送一次数据,那么默认值就可以了.但是,如果您使用SSE处理不规则数据,并且消息之间有时可能会有半小时,请确保超时超过半小时.
更新:显然(请参阅注释)添加response.addHeader("X-Accel-Buffering", "no");(即到服务器端进程,而不是代理配置)修复问题.这是有道理的,因为它是专门为SSE和类似的HTTP流添加的,请参阅nginx文档.它确实意味着上面的Nginx配置也应该工作(OP报告说它没有).但是,另一方面,使用标头在每个连接的基础上禁用缓冲感觉就像一个更好的解决方案.
| 归档时间: |
|
| 查看次数: |
4747 次 |
| 最近记录: |