Nginx 常用配置
一、常用反向代理配置
server {
listen 8899; #监听端口
server_name _;
location /api/ {
# 这里配置代理到后端服务的地址
proxy_pass http://127.0.0.1:19009/;
}
location / {
# 这里配置前端资源的路径
root /usr/local/project/invoice/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
二、图片代理配置
server {
listen 8848;
server_name _;
location /mac {
limit_req zone=mylimit burst=5;
# 这里配置前端资源的路径
alias /usr/local/project/mac-fxb/images/;
client_max_body_size 20M; # 设置请求实体最大大小为20MB
}
location /macPro {
limit_req zone=mylimit burst=5;
# 这里配置前端资源的路径
alias /usr/local/project/mac-fxb/imagesPro/;
client_max_body_size 20M; # 设置请求实体最大大小为20MB
}
location / {
limit_req zone=mylimit burst=5;
# 这里配置前端资源的路径
alias /usr/local/project/invoice/images/;
client_max_body_size 20M; # 设置请求实体最大大小为20MB
}
# 定义一个用于返回 403 Forbidden 错误的 location
error_page 403 /403.html;
location = /403.html {
root /etc/nginx/;
internal; # 表示这个 location 只能内部重定向,不能直接访问
}
}
三、长连接代理配置
server {
listen 19008;
server_name _;
location /dev-api/ {
proxy_pass http://127.0.0.1:8080/;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
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;
}
location / {
root D:\\OOP2\\project\\SDP\\smart-health-care\\ruoyi-ui\\dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
}
四、Nginx开启gzip压缩及请求频率限制
user nginx;
worker_processes auto;
worker_rlimit_nofile 3000;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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;
types_hash_max_size 4096;
send_timeout 200s;
keepalive_timeout 200s;
keepalive_requests 5000;
server_tokens off;
gzip on;
gzip_min_length 2k;
gzip_disable msie6;
gzip_types text/css application/javascript text/javascript image/jpeg image/png image/gif;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
client_max_body_size 1000M;
client_body_buffer_size 1000M;
fastcgi_intercept_errors on;
limit_req_zone $binary_remote_addr zone=mylimit:150m rate=28r/s;
limit_req_status 403;
server {
location /dev-api/ {
limit_req zone=mylimit burst=15;
add_header 'Access-Control-Allow-Origin' 'https://www.xxxx.cn';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_pass http://127.0.0.1:19008/;
}
location / {
root /usr/local/project/mac-fxb/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location ~ \.(jpg|jpeg|png|gif|svg)$ {
proxy_pass http://xxxx.cn:8848;
proxy_set_header Host $host;
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 http;
}
error_page 403 /403.html;
location = /403.html {
root /etc/nginx/;
internal;
}
}
}
五、nginx 子路径配置
location /health-care {
alias /usr/local/project/item_ruoyi/dist;
index index.html index.htm;
try_files $uri $uri/ /health-care/index.html;
}