Nginx相关
安装Nginx
shell
$ sudo apt install nginx -y检查 Nginx 状态
shell
$ sudo systemctl status nginx允许 HTTP 访问(80 端口)
shell
$ sudo ufw allow 'Nginx HTTP'若需要 HTTPS(443 端口),再运行:
shell
$ sudo ufw allow 'Nginx HTTPS'查看防火墙规则是否生效
shell
$ sudo ufw status检查配置是否有误
shell
$ sudo nginx -t启动 Nginx
shell
$ sudo systemctl start nginx停止 Nginx
shell
$ sudo systemctl stop nginx重启 Nginx(配置修改后需执行)
shell
$ sudo systemctl restart nginx平滑重载配置(不中断服务)
shell
$ sudo systemctl reload nginx设置开机自启动
shell
$ sudo systemctl enable nginxNginx配置
shell
# 配置80端口http服务
server {
listen 80;
server_name ip地址;
root /var/www/xxx;
index index.html index.htm;
# xxx
location /xxx {
alias /var/www/xxx;
index index.html;
try_files $uri $uri/ /xxx/index.html;
}
location / {
try_files $uri $uri/ =404;
}
}
# 配置443端口https服务
server {
listen 443 ssl;
server_name wyfe.top;
# 获取/etc/nginx/ssl/wyfe.top目录下的公私钥
ssl_certificate /etc/nginx/ssl/wyfe.top/scs1776215303444_www.wyfe.top_server.crt;
ssl_certificate_key /etc/nginx/ssl/wyfe.top/scs1776215303444_www.wyfe.top_server.key;
# 强制HTTP跳转到HTTPS(可选)
# if ($scheme = http) {
# return 301 https://$server_name$request_uri;
# }
# 静态资源转发到 Node
location /public/ {
alias /var/www/wyfe-iapi/public/;
expires 30d; # 缓存优化
}
# 反向代理接口请求到 Koa 服务
location /api/ { # 匹配所有以 /api 开头的接口
proxy_pass http://127.0.0.1:6123/api/; # 转发到本地 8805 端口的 Koa 服务
proxy_http_version 1.1; # 使用 HTTP/1.1 协议,支持长连接和 WebSocket
# 用于 WebSocket 代理的关键配置(若无需 WebSocket 可保留或删除)
proxy_set_header Upgrade $http_upgrade; # 传递 Upgrade 头,标识是否升级协议
proxy_set_header Connection 'upgrade'; # 告知后端需要升级连接(如 WebSocket)
proxy_set_header Host $host; # 传递客户端请求的 Host 头,避免后端获取到代理服务器的 Host
proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实 IP 给后端
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 记录代理链 IP(多层代理时有效)
proxy_set_header X-Forwarded-Proto $scheme; # 传递客户端请求的协议(http/https)
proxy_cache_bypass $http_upgrade; # 当需要升级协议时(如 WebSocket),跳过缓存
# 超时设置(避免后端处理慢导致连接被断开)
proxy_connect_timeout 30s; # 与后端建立连接的超时时间
proxy_send_timeout 60s; # 向后端发送请求的超时时间
proxy_read_timeout 60s; # 等待后端响应的超时时间
# 缓冲区设置(优化大请求处理)
proxy_buffering on; # 启用缓冲区
proxy_buffer_size 16k; # 缓冲区大小
proxy_buffers 4 64k; # 缓冲区数量和每个大小
}
root /var/www/wyfe;
index index.html;
autoindex off;
# /
location / {
try_files $uri $uri/ /index.html;
}
# idocs
location /idocs {
alias /var/www/idocs;
index index.html;
location ~ ^/idocs/ivue/?$ {
return 301 /idocs/ivue/introduction.html;
}
location ~ ^/idocs/iutils/?$ {
return 301 /idocs/iutils/introduction.html;
}
try_files $uri $uri/ /idocs/index.html;
}
# ivue
location /ivue {
alias /var/www/ivue;
index index.html;
try_files $uri $uri/ /ivue/index.html;
}
# icode
location /icode {
alias /var/www/icode;
index index.html;
try_files $uri $uri/ /icode/index.html;
}
# iadmin
location /iadmin {
alias /var/www/iadmin;
index index.html;
try_files $uri $uri/ /iadmin/index.html;
}
# store/vue3-admin
location /store/vue3-admin {
alias /var/www/store/vue3-admin;
index index.html;
try_files $uri $uri/ /store/vue3-admin/index.html;
}
# store/datascreen1
location /store/datascreen1 {
alias /var/www/store/datascreen1;
index index.html;
try_files $uri $uri/ /store/datascreen1/index.html;
}
# store/datascreen2
location /store/datascreen2 {
alias /var/www/store/datascreen2;
index index.html;
try_files $uri $uri/ /store/datascreen2/index.html;
}
# store/museum
location /store/museum {
alias /var/www/store/museum;
index index.html;
try_files $uri $uri/ /store/museum/index.html;
}
# store/qunar
location /store/qunar {
alias /var/www/store/qunar;
index index.html;
try_files $uri $uri/ /store/qunar/index.html;
}
}