前端页面nginx配置:
# 定义变量
include /www/wwwroot/api.test.com/server_before.conf;
server
{
listen 80;
listen 443 ssl;
listen 443 quic;
http2 on;
server_name www.test.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/www.test.com-new;
#CERT-APPLY-CHECK--START
# 用于SSL证书申请时的文件验证相关配置 -- 请勿删除
include /www/server/panel/vhost/nginx/well-known/www.test.com.conf;
#CERT-APPLY-CHECK--END
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/www.test.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/www.test.com/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
add_header Alt-Svc 'quic=":443"; h3=":443"; h3-29=":443"; h3-27=":443";h3-25=":443"; h3-T050=":443"; h3-Q050=":443";h3-Q049=":443";h3-Q048=":443"; h3-Q046=":443"; h3-Q043=":443"';
error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
#清理缓存规则
location ~ /purge(/.*) {
proxy_cache_purge cache_one "$host(pc|mobile)$1$is_args$args";
access_log /www/wwwlogs/www.test.com_purge_cache.log;
}
location ~ /yandex_aaa.html {}
location ~ /robots.txt {}
location ~ /googlebbb499c.html {}
#引用反向代理规则,注释后配置的反向代理将无效
#include /www/server/panel/vhost/nginx/proxy/www.test.com/*.conf;
include enable-php-00.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
#include /www/server/panel/vhost/rewrite/www.test.com.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
#禁止在证书验证目录放入敏感文件
if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
return 403;
}
# 加载重定向规则
include /www/wwwroot/api.test.com/redirects.conf;
# 匹配首页
# location = /
# {
# include /www/wwwroot/api.test.com/proxy.conf;
# # 设置缓存有效期为1天
# proxy_cache_valid 200 304 301 302 1d;
# # 添加Cache-Control头部,指示客户端缓存120秒
# add_header Cache-Control "public, max-age=86400";
# }
# 在 server 块中添加以下配置, 完整列表:https://www.cloudflare-cn.com/ips/
set_real_ip_from 173.245.48.0/20; # Cloudflare IPv4 CIDR
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 2400:cb00::/32; # Cloudflare IPv6 CIDR
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
real_ip_header CF-Connecting-IP; # 使用 Cloudflare 提供的真实 IP 头
real_ip_recursive on;
location /
{
proxy_pass http://localhost:8041;
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 REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
# 1. 定义设备类型变量(默认PC)
set $device_type "pc";
# 2. 通过User-Agent识别移动设备(匹配常见移动设备标识)
if ($http_user_agent ~* "(android|iphone|ipad|ipod|mobile|windows\s+phone|mqqbrowser|micromessenger)") {
set $device_type "mobile";
}
# 静态资源缓存1天
set $static_fileDCuRT9sR 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
set $static_fileDCuRT9sR 1;
expires 30d;
}
if ( $static_fileDCuRT9sR = 0 )
{
#add_header Cache-Control no-cache;
add_header Cache-Control "public, max-age=120";
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
# 3. 缓存键中加入设备类型(关键修改)
proxy_cache_key $host$device_type$uri$is_args$args;
proxy_cache_valid 200 304 301 302 30m;
}
access_log /www/wwwlogs/www.test.com.log;
error_log /www/wwwlogs/www.test.com.error.log;
}
API接口伪静态:
location ~* (runtime|application)/{
return 403;
}
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}