nginx本地代理配置访问node服务
仅供参考。
1、node修改
代码修改略(关键是不懂),相关server.js配置:
$ cat server.js
const express = require("express");
const app = express();
const path = require("path");
app.use(function (req, res, next) {
if (req.url.startsWith("/fire_static/css/") || req.url.startsWith("/fire_static/js/") || req.url.startsWith("/fire_static/img/") || req.url.startsWith("/fire_static/fonts/")) {
return next()
}
return res.sendFile(path.resolve("fire/dist/index.html"))
});
app.use("/", express.static(path.resolve("fire/dist")));
app.listen(4009);
2、node目录结构:
[app@VHOST-121 node-xf]$ ls
fire node_modules package-lock.json server.js
[app@VHOST-121 node-xf]$ pwd
/data/Application/node-xf
[app@VHOST-121 node-xf]$ ls
fire node_modules package-lock.json server.js
[app@VHOST-121 node-xf]$ cd fire/
[app@VHOST-121 fire]$ pwd
/data/Application/node-xf/fire
[app@VHOST-121 fire]$ ls
dist
[app@VHOST-121 fire]$ cd dist/
[app@VHOST-121 dist]$ pwd
/data/Application/node-xf/fire/dist
[app@VHOST-121 dist]$ ls
favicon.ico fire_static index.html
[app@VHOST-121 dist]$ cd fire_static/
[app@VHOST-121 fire_static]$ pwd
/data/Application/node-xf/fire/dist/fire_static
[app@VHOST-121 fire_static]$ ls
css fonts img js
3、本地nginx配置:
server {
listen 19001 default_server;
server_name node-fire;
root /data/Application/node-xf/fire/dist;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://192.168.1.11:4009;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_http_version 1.1;
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 Host $host:$server_port;
}
location /fire_static {
root /data/Application/node-xf/fire/dist;
}
}
4、前端nginx配置:
upstream node-fire-CGMS-HJYS-houjie {
server 192.168.1.121:19001;
}
server {
listen 16666;
ssl on;
server_name localhost;
add_header X-Frame-Options SAMEORIGIN;
ssl_certificate bundle.crt;
ssl_certificate_key com.cn.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
proxy_redirect http:// $scheme://;
port_in_redirect on;
client_max_body_size 300m;
error_page 500 502 503 504 /error.html;
location = /50x.html {
root html;
}
location /fire {
proxy_pass http://node-fire-CGMS-HJYS-houjie;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_http_version 1.1;
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 Host $host:$server_port;
}
}
注:因“location /fire_static ”与“ location /fire” 前半截相同,故运行中无异常,实际目前的配置是有缺陷的,待有问题时再研究;