我正在尝试设置nginx conf以便与同一台服务器中的多节点应用程序一起使用。
我想使用:
http:// localhost / node-app-01以访问端口3001中的app-01
http:// localhost / node-app-02可以访问端口3002中的app-02,依此类推。
但是它不起作用。
错误是“ http://localhost/css/chunk-006c7b90.0199750b.css net::ERR_ABORTED 404 (Not Found)
”。我可以看到该端口不存在。
如果我使用http:// localhost:3001,http:// localhost:3002访问该应用程序...一切正常。
如果我使用来运行应用程序
我的文件夹应用程序结构:
\nginx
\conf
\html
\logs
....
\dev-folder
\dist
| index.html
| \css
| css files
| \js
| js files
|
|\node-app-01 /*run in localhost:3001*/
| \node_modules
| node module files
| \public
| public app files
| package.json
| app.js
| server.js
|\node-app-02 /*run in localhost:3002*/
| \node_modules
| node module files
| \public
| public app files
| package.json
| app.js
| server.js
|\node-app-03 /*run in localhost:3003*/
| \node_modules
| node module files
| \public
| public app files
| package.json
| app.js
| server.js
Nginx conf:
http {
include mime.types;
default_type application/octet-stream;
....
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#nginx original server from install
location / {
root html;
index index.html index.htm;
}
location ^~ /node-app-01/ {
rewrite ^/node-app-01/(.*)$ /$1 break;
proxy_pass http://localhost:3001/;
}
location ^~ /node-app-02/ {
rewrite ^/node-app-02/(.*)$ /$1 break;
proxy_pass http://localhost:3002/;
}
location ^~ /node-app-03/ {
rewrite ^/node-app-03/(.*)$ /$1 break;
proxy_pass http://localhost:3003/;
}
}
//
答案 0 :(得分:0)
您可以在nginx配置中定义上游,如果您有websocket,请尝试使用ip_hash选项
看看https tls配置中的示例:
upstream express_servers {
ip_hash;
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 443 ssl;
server_name mydomain.com;
error_log on;
ssl_certificate /home/test/ssl/fullchain.pem;
ssl_certificate_key /home/test/ssl/privkey.pem;
client_body_timeout 3m;
client_header_timeout 3m;
client_max_body_size 150m;
send_timeout 3m;
proxy_set_header X-Real-IP $remote_addr; # pass on real client IP
location / {
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;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://express_servers;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}