![NGINX 在同一个域中托管多个服务器](https://linux22.com/image/782682/NGINX%20%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E5%9F%9F%E4%B8%AD%E6%89%98%E7%AE%A1%E5%A4%9A%E4%B8%AA%E6%9C%8D%E5%8A%A1%E5%99%A8.png)
我有一个由 Nginx 提供服务的网站,我正在尝试做一些事情,我不知道是否可以按照我尝试的方式去做,但情况是:
我有用 React / Node 制作的应用程序 A 和 B。
- 应用程序 A 是主要的,它具有 Dockerized 上的前端和后端。
- 应用程序 B 是管理面板,它具有单独的前端、后端并且也是 Docker 化的。
- 我目前正在处理申请Awww.applicationA.com/,我正在尝试在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
当无法到达指定的目标时,nginx 将返回 502 Bad Gateway 错误proxy_pass
。在这种情况下,请确保 nginx 运行时可以访问 5001 端口。