重温 Nginx 的匹配规则
location 的末尾是否带 “/”(斜杠)
带了斜杠 :如:/aa/ 。严格路径匹配 /aa/* 之后的一些路径。
不带斜杠 :如:/aa。匹配:/aa* (/aabb、/aa/bb、/aaccdd/dd)只要是前缀是以"location“开头的,而不是严格路径匹配
proxy_pass 的末尾是否带 “/”(斜杠)
带了斜杠 :转发后的路径 = proxy_pass 的路径 + ( 客户端请求的完整路径 - location 匹配到的路径 )
不带斜杠 :转发后的路径 = proxy_pass 的路径 + ( 客户端请求的完整路径 )
结论
替换路径,"location" 和 "proxy_pass" 两端必须带上斜杠 “/”(斜杠)
yaml
location /api/ {
proxy_pass http://127.0.0.1:8080/v1/;
}
nginx 如何配置主应用与子应用
主应用配置
shuser nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 8080;
listen [::]:8080;
server_name fs_h5_server;
gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/javascript text/javascript image/svg+xml;
gzip_vary on;
include /etc/nginx/fs.conf.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
// 所有子应用公用代理端口
location /api/v1 {
proxy_pass http://192.168.18.151:8082;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /public/api/ {
rewrite ^/fs-api/(.*)$ /$1 break;
proxy_pass https://open.feishu.cn;
proxy_set_header Host open.feishu.cn;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1.2 TLSv1.3;
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;
}
}
}
子应用配置
子应用配置1
bash
location /child1/ {
alias /usr/local/nginx/html/child1/;
index index.html;
try_files $uri $uri/ /child1/index.html;
expires 30d;
add_header Cache-Control "public, no-transform";
if ($request_filename ~* \.(html|htm)$) {
expires -1;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
}
子应用配置2
shlocation /child2/ {
alias /usr/local/nginx/html/child2/;
index index.html;
try_files $uri $uri/ /child2/index.html;
expires 30d;
add_header Cache-Control "public, no-transform";
if ($request_filename ~* \.(html|htm)$) {
expires -1;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
}
location /api/v1/xxx {
proxy_pass https://aaa.cn;
proxy_ssl_server_name on; // 开启 ssl server
proxy_ssl_name aaa.cn;
proxy_set_header Host aaa.cn
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}