我有一个默认的 Laravel应用程序在端口80上运行。它使用PHP 7的默认设置。现在,我想添加另一个通常运行在端口3000上的nodejs应用程序,并在Nginx上从端口8000到3000使用反向代理。但是事实证明这是502错误的网关。这是我的pm2状态
┌──────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ FAM │ 0 │ 1.0.0 │ fork │ 16073 │ online │ 0 │ 97m │ 0% │ 48.4 MB │ root │ disabled │
└──────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
我怀疑502的真正原因是因为用户是root用户,所以我将所有js文件所有权更改为
-rw-r--r-- 1 apache apache 273 Nov 13 16:19 README.md
drwxr-xr-x 4 apache apache 4096 Nov 14 21:28 components
drwxr-xr-x 2 apache apache 4096 Nov 14 21:28 core
-rw-r--r-- 1 apache apache 693 Nov 15 17:30 ecosystem.config.js
-rw-r--r-- 1 apache apache 508 Nov 14 21:28 env.js
drwxr-xr-x 798 root root 28672 Nov 15 21:40 node_modules
-rw-r--r-- 1 apache apache 372088 Nov 15 21:40 package-lock.json
-rw-r--r-- 1 apache apache 1971 Nov 14 21:28 package.json
-rw-r--r-- 1 apache apache 1621 Nov 14 21:28 server.js
drwxr-xr-x 5 apache apache 4096 Nov 14 21:28 static
drwxr-xr-x 3 apache apache 4096 Nov 14 21:28 styles
我已经在firewall-cmd
上公开了8000端口,并将其添加到/etc/service
中。但这一切都失败了。
这是nginx默认配置(/etc/nginx/nginx.conf)
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/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;
upstream fam_upstream {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 80;
listen [::]:80 default_server;
server_name _;
root /var/www/html/laravel/public;
index index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
# include fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
# fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
供您参考,此VPS尚未设置域名。因此,它们全部在常规http上运行。
这是8000端口的conf
upstream fam-app {
server <VPS_IP>:3000;
}
server {
listen 8000;
location / {
proxy_set_header X-NginX-Proxy true;
proxy_pass http://fam-app/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_max_temp_file_size 0;
proxy_read_timeout 240s;
}
}
大多数解决方案都显示了如何在Ubuntu上运行pm2和nginx,但对Centos 7却知之甚少。