
我有一個由 Nginx 提供服務的網站,我正在嘗試做一些事情,我不知道這是否可能像我嘗試做的那樣,但情況是:
我有使用 React / Node 製作的應用程式 A 和 B。
- 應用程式A是主要的,它有Dockerized上的前端和後端。
- 應用程式 B 是管理面板,它具有單獨的前端、後端,也是 Docker 化的。
- 我目前正在為應用程式 A 提供服務www.applicationA.com/,我正在嘗試為應用程式 B 提供服務www.applicationA.com/admin。
下面是我的 NGINX 配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name applicationA.com www.applicationA.com;
root /var/www/html/;
location / {
// *This is working*
rewrite ^ https://$host$request_uri? permanent;
proxy_pass http://applicationA.com:8080;
}
location /admin {
// *This is **NOT** working*
proxy_pass http://applicationA.com:5001;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name applicationA.com www.applicationA.com;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/applicationA.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/applicationA.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
location / {
try_files $uri @server;
}
location @server { // **Working**
proxy_pass http://applicationA.com:8080;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
}
location /admin { // **NOT Working**
proxy_pass http://applicationA.com:5001;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval'
'unsafe-inline'" always;
}
root /var/www/html/;
index index.html index.htm index.nginx-debian.html;
}
- 我訪問 applicationA.com/admin 得到的回應是 502:錯誤網關。
- 日誌中沒有任何相關內容指控任何錯誤。
- 如果需要,我將發布 Node 檔案或 docker-compose 檔案。
答案1
proxy_pass
當無法到達指定的目的地時,nginx 會傳回 502 Bad Gateway 錯誤。在這種情況下,請確保可以從執行 nginx 的位置存取 5001 連接埠。