![](/img/trans.png)
[英]JSESSIONID collision between two servers on same ip but different ports
[英]How to use nginx to direct traffic to two different ports on the same device?
我目前正在从事一个 FPV 机器人项目,该项目有两台服务器,flask/werkzeug 和 streamserver,为 http 流量和流式视频提供服务,并将视频流式传输到位于另一台机器上的外部 web 服务器。
目前的配置方式是这样的:
我想将它们放在 https 反向代理后面,以便我可以通过https://example.com连接到它,其中“example.com”在我的外部系统主机文件 2 中设置为 1。
我想:
. . . 这样“外部”连接(就外部世界而言,到端口 5000 和 5001 都是安全连接,例如:
[external system]-https://example.com:5000/5001----nginx----https://example.com:5000
\---http://example.com:5001
http://example.com:5000 or 5001 redirects to https.
到目前为止,我看到的所有文献都在谈论:
我的应用程序只是一个日常普通的普通服务器类型配置,而我什至弄乱 https 的唯一原因是因为除了在安全上下文中无法完成我的项目之外,事情无法正常工作的真正烦人的问题。
我确信这是可能的,但文献要么令人困惑,要么似乎在谈论不同的用例。
参考简单的操作方法将是最有用的选择。 清晰明确的步骤也将受到赞赏。
提前感谢您提供的任何帮助。
这个最小配置应该提供公共端点:
http://example.com/* => https://example.com/*
https://example.com/stream => http://1.2.3.4:5001/
https://example.com/* => https://1.2.3.4:5000/
# redirect to HTTPS
server {
listen 80;
listen [::]:80;
server_name example.com
www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com
www.example.com;
ssl_certificate /etc/nginx/ssl/server.cer;
ssl_certificate_key /etc/nginx/ssl/server.key;
location /stream {
proxy_pass http://1.2.3.4:5001/; # HTTP
}
# fallback location
location / {
proxy_pass https://1.2.3.4:5000/; # HTTPS
}
}
首先,应得的信用:@AnthumChris 的回答基本上是正确的。
经过大量的额外研究,我发现了以下内容:
网上的信息其实太多了,大部分都是自相矛盾的,可能是错误的,不必要的复杂。
安装 nginx:
使用 nginx 配置系统并连接到它:
注意:这是我的用例独有的特殊情况,因为它在独立机器人上运行以用于开发目的,并且我的域不是面向 Web 的服务器上的“实时”域。 它是一个“真实”域,具有“真实”和受信任的证书,以避免在开发过程中出现浏览器警告。
配置 nginx:
/etc/nginx/sites-enabled
nginx -T
是你的朋友。 您可以在尝试启动之前使用它来“测试”您的配置是否存在问题。sudo systemctl restart nginx
将尝试重新启动 nginx,(在您开始配置时,可能会失败。)sudo systemctl status nginx.service >./[path]/log.txt 2>&1
也是你的朋友。 这允许您在运行时收集将阻止服务启动的错误消息。 就我而言,大多数问题是由使用我选择的端口的其他服务或愚蠢的错误配置引起的。sudo netstat -tulpn | grep nginx
sudo netstat -tulpn | grep nginx
以确保它正在侦听正确的端口。nginx 运行后故障排除:
SSL 证书:
cat mycert.crt bundle.file > combined.crt
将站点证书与从证书颁发机构收到的中间证书捆绑包结合起来创建它。最终我得到了以下配置文件:
#server {
# listen example.com:80;
# server_name example.com;
# return 301 https://example.com$request_uri;
# }
# This is the "web" server (command and control), running Flask/Werkzeug
# that must be passed through as a secure connection so that the
# joystick/gamepad works.
#
# Note that the internal Flask server must be configured to use a
# secure connection too. (Actually, that may not be true, but that's
# how I set it up. . .)
#
server {
listen example.com:443 ssl;
server_name example.com;
ssl_certificate /usr/local/share/ca-certificates/extra/combined.crt;
ssl_certificate_key /usr/local/share/ca-certificates/extra/example.com.key;
ssl_prefer_server_ciphers on;
location / {
proxy_pass https://example.com:5000;
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;
}
}
# This is the video streaming port/server running streamserver
# which is not, and cannot be, secured. However, since most
# modern browsers will not mix insecure and secure content on
# the same page, the outward facing connection must be secure.
#
server {
listen example.com:5001 ssl;
server_name example.com;
ssl_certificate /usr/local/share/ca-certificates/extra/combined.crt;
ssl_certificate_key /usr/local/share/ca-certificates/extra/www.example.com.key;
ssl_prefer_server_ciphers on;
# After securing the outward facing connection, pass it through
# as an insecure connection so streamserver doesn't barf.
location / {
proxy_pass http://example.com:5002;
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.