![](/img/trans.png)
[英]How to reverse proxy to a Node.js app in a domain subfolder with Nginx on CentOS?
[英]Nginx as reverse proxy for Node.js app on CentOS 7
我有一個在服務器port 5000
上運行的節點應用程序,可以從我的瀏覽器訪問。 未安裝 Apache。 我遵循了安裝和配置 Nginx 的指南,我能夠成功打開為 centos Nginx 安裝服務的默認 HTML 頁面,這意味着端口 80 已打開(還沒有 SSL)。 然后我通過添加以下內容來編輯/etc/nginx/nginx.conf
的location
設置:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://localhost:5000;
}
在使用sudo systemctl restart nginx
成功重啟之后,我現在看到了一個默認錯誤頁面,該頁面是 nginx 安裝附帶的,而不是我在 server_ip:5000 上運行的應用程序。 如何正確調試它並找出反向代理不起作用的原因?
我的完整 nginx.conf (我只刪除了評論的 SSL 設置):
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://localhost:5000;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
請更新您的server
塊,如下所示:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5000;
proxy_redirect off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
另外,請確保您的應用程序正在偵聽localhost:5000
。 有時,服務只綁定到接口 IP,而不綁定到 localhost。
如果您仍然遇到問題,請檢查您的 nginx 錯誤日志並提供日志中的錯誤消息。
與 nginx 配置文件一起,這是默認禁用的 selinux 模塊httpd_can_network_connect
的問題。 您可以通過以下命令啟用它:
setsebool -P httpd_can_network_connect on
更多詳情可在https://wiki.centos.org/TipsAndTricks/SelinuxBooleans找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.