Nginx 怎么设置 wordpress 伪静态?非宝塔面板

2023-04-2712:12:14服务器及运维Comments1,563 views字数 819阅读模式
作者:书中枫叶文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/37570.html

Nginx 中设置 WordPress 的伪静态(Permalinks)需要通过配置 Nginx 的服务器块(server block)来实现。下面是一个简单的示例配置,演示如何启用 WordPress 的伪静态功能:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/37570.html

server {
    listen 80;
    server_name your-domain.com; # 替换成你的域名

    root /var/www/html; # 替换成你的 WordPress 安装目录

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 替换成你的 PHP-FPM Socket 路径
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # 以下是伪静态规则
    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
        }
    }

    # WordPress 静态资源缓存配置,如果你使用了静态资源缓存插件,请根据插件的要求配置
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }
}

上面的配置中,关键部分是 location / 后面的伪静态规则,使用了 rewrite 指令将 URL 中的请求重写到 index.php 文件,并传递了请求参数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/37570.html

请注意,上面的示例配置仅供参考,你需要根据你的具体服务器和 WordPress 安装情况进行适配和调整。另外,如果你使用了静态资源缓存插件(如 WP Super Cache、W3 Total Cache 等),你还需要根据插件的要求配置相应的 Nginx 缓存规则,以确保静态资源缓存正常工作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/37570.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/37570.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/37570.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定