Hugo博客公告弹窗

宝塔面板:苹果cms系统站点最佳Nginx配置

   
文章摘要
摘要小助理今天溜号啦……😜

环境

系统:debian11

宝塔:7.7.0

nginx: 1.26.1 (编译)

mysql: 5.7

php: 7.4

关于怎么配置Nginx FastCGI 缓存 本博客有教程

适用苹果cms站点:https://github.com/woniu336/open_shell/blob/main/maccms.conf

适用多数站点:https://github.com/woniu336/open_shell/blob/main/nginx.conf

https://pastebin.com/TqmpxGfh

Nginx完整配置

注意:

  • 这不是nginx的配置,而是站点的配置
  • 每个站点创建不同的缓存目录,设置不同的key

创建缓存目录

注意:最好是在/var/cache/目录下创建,否则重启服务器会导致nginx无法启动

mkdir -p /var/cache/nginx/catpdf_org
chmod -R 755 /var/cache/nginx/catpdf_org

完整配置:

注意:

  • 替换自己的域名,站点目录,证书目录,php版本,苹果cms后台地址(houtai.php)
  • 必须有且只有一个站点是:listen 443 quic reuseport; 后面多了一个reuseport
  • 其他站点是listen 443 quic;
  • catpdf_org 是缓存目录,CATPDF.ORG是key
fastcgi_cache_path /var/cache/nginx/catpdf_org levels=1:2 keys_zone=CATPDF.ORG:200m inactive=2d max_size=10G;
fastcgi_cache_key "$scheme$request_method$host$request_uri$is_args$args";
fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

server {
    listen 80;
    listen 443 ssl;
    listen 443 quic;
    http2 on;
    http3 on;
    quic_gso on;
    quic_retry off;

    server_name baidu.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/baidu.com;

    # SSL 配置
    ssl_certificate /www/server/panel/vhost/cert/baidu.com/fullchain.pem;
    ssl_certificate_key /www/server/panel/vhost/cert/baidu.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1h;
    ssl_early_data on;
    ssl_stapling on;
    ssl_stapling_verify on;

# 配置 Nginx fastcgi_cache START
set $skip_cache 0;

# 定义不缓存的条件
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($request_uri ~* "purge=all|/wp-admin/|/xmlrpc.php|houtai.*\.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; }
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; }

# 后台路径重写规则
location /houtai.php/admin/ {
    rewrite ^/houtai\.php/admin/(.*)$ /houtai.php?s=/admin/$1 last;
}

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/tmp/php-cgi-74.sock;
    fastcgi_index index.php;
    include fastcgi.conf;  
 
    fastcgi_param PATH_INFO $fastcgi_path_info;
    # 缓存规则
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache CATPDF.ORG;
    fastcgi_cache_valid 200 301 302 1d;

    # 安全头部
    add_header Strict-Transport-Security "max-age=15552000; includeSubdomains; preload";
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Cache "$upstream_cache_status From $host";
    add_header Cache-Control "max-age=86400";
    add_header Nginx-Cache "$upstream_cache_status";
    add_header Last-Modified $date_gmt;

    etag on;
}

# 缓存清理配置
location ~ /purge(/.*) {
    allow 127.0.0.1;
    allow "47.83.27.48";
    deny all;
    fastcgi_cache_purge CATPDF.ORG "$scheme$request_method$host$1";
}
# 配置 Nginx fastcgi_cache END


    # HTTP重定向至HTTPS
    if ($server_port !~ 443) {
        return 301 https://$host$request_uri;
    }

    # Proxy headers
    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 Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_buffering off;
    proxy_http_version 1.1;
    proxy_read_timeout 600s;

    # HTTP/3 headers
    add_header Alt-Svc 'h3=":443"; ma=2592000';
    add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload";

    # Error pages
    error_page 497 https://$host$request_uri;

    # PHP 配置
    include enable-php-74.conf;

    # Rewrite rules
    include /www/server/panel/vhost/rewrite/baidu.com.conf;

    # 禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) {
        return 404;
    }

    # 一键申请SSL证书验证目录相关设置
    location ~ \.well-known {
        allow all;
    }

    # Static file handling
    location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
        access_log off;
    }

    location ~* \.(js|css)?$ {
        expires 12h;
        access_log off;
    }

    # Logging
    access_log /www/wwwlogs/baidu.com.log;
    error_log /www/wwwlogs/baidu.com.error.log;
}

删除缓存

添加宝塔计划任务,任务类型:shell脚本

rm -rf /var/cache/nginx/catpdf_org/*
CC BY-NC-SA 4.0 转载请注明
最后更新于 2024-11-23 15:12
clarity统计