[英]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.