反向代理websocket
For NGINX to send the Upgrade request from the client to the backend server, the
Upgrade
andConnection
headers must be set explicitly, as in this example:
location /wsapp/ {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
Linux中配置
昨天在搭建测试环境时,一直无法转发请求,配置如下
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://dingtalk_springboot:8080;
}
}
按照一篇博客修《nginx 反向代理用做内网域名转发》修改后成功
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://dingtalk_springboot:8080;
}
}
主要时proxy_set_header Host $host;
起作用
这一行的作用是把原http请求的Header中的Host字段也放到转发的请求里 而服务器是靠这个Host值来区分你请求的是哪个域名的资源的。
代理的后端服务器可以通过 Host 头得知用户访问的真正的域名, 如不设置, 则得到是代理服务(nginx)的 ip, 这样对于动态拼接的 url,后端服务器能在页面里返回正确的 url.
参考:
windows中配置
- 无法重新加载的问题
- 输入
nginx -c conf/nginx.conf
后,再重载 - nginx报错[error] CreateFile()