【问题标题】:Nginx: WordPress Rewrites not Working unless I do bad thingsNginx:除非我做坏事,否则 WordPress 重写不起作用
【发布时间】:2015-04-28 20:19:42
【问题描述】:

我最近刚从 Apache 迁移到 Nginx,我遇到了 Wordpress 和 Nginx 的障碍。目前我正在运行一个更简单的 Nginx conf,这样您就可以更轻松地查看发生了什么,而无需再查看 50 行以上的代码。

server {
    listen          80;
    server_name     servername.com
    root            /home/sname/www;

    # IF I DON'T DO THIS ALL PAGES APART FROM THE HOMEPAGE DON'T APPEAR!
    # Further to this, I use a custom permalink structure %post_name%.
    # When turned off the pages work but I can't use custom permalink structure
    error_page 404 /index.php;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }

    # PHP Handler
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

我拥有 HTTP 块中的所有常规:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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;
    keepalive_timeout  65;

    index   index.php index.html index.htm;

    include /etc/nginx/conf.d/*.conf;

为什么我不能忍受这个?好吧,这是一个站点范围的示例。 (在站点名称中替换!由于错误的 URL 和链接限制)

http://www.pro-tradesouthwales.co.uk/services/damp-proofing/

网站名称!/services/damp-proofing/index.php/index.php

任何和所有的帮助表示赞赏。我已经尝试了很多不同的设置,并试图让它工作好几天。请不要光顾我说读指南之类的话,我吃了该死的 nginx 字典仍然无济于事:)

谢谢大家!

【问题讨论】:

  • 如果删除 try_files $uri =404; 会发生什么PHP处理程序中的行?另外,您在任何地方都有大致的位置/街区吗?

标签: wordpress nginx url-rewriting


【解决方案1】:

让它在一定程度上发挥作用。原来 WordPress 只是有问题,但据我所知,这是设置 WordPress 网站的最“正确”方法:)

希望它也对其他人有所帮助。请记住,事情会根据您的服务器和独特的设置而变化。所以你的 PHP 设置可能会有所不同。例如 php-fpm 目录并不总是存在,有时也被创建为 php5-fpm。如果有疑问,请使用 shell(使用 putty 或类似工具通过 SSH 连接到您的服务器)并找到 php-fpm.sock。

又名: 光盘 /etc/ 目录 -a ---- 显示文件列表 ---- ... 等等

    server {
        listen       80;
        server_name  www.pro-tradesouthwales.co.uk;
        root         /home/protrade/www;

        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        
        # START: Solution
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        # END: Solution

        location ~ /\. {
                deny all;
        }

        location ~* /(?:uploads|files)/.*\.php$ {
                deny all;
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~* \.(js|css|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                expires max;
                log_not_found off;
        }

        location ~* \.()$ {
               log_not_found off;
               expires max;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多