首页 > 解决方案 > Nginx + Wordpress + 永久链接 | 服务器块的奇怪行为

问题描述

这两天我正在尝试所有可能的配置。我已经走到了一半,但从来没有走到尽头。

在我的硬盘驱动器死后,它在远程服务器上安装了全新的 Debian 9.14。MariaDB + php7.0-fpm + nginx 1.10.3

我可以访问主页和 Wp-admin,但在自定义永久链接中,所有其他页面都是 404。当永久链接为普通链接时,我可以访问所有内容。

我有几个网站,所有人都有同样的问题。

我的文件夹结构是:/var/www/website1/ && /var/www/website2/ 等...

这是我的 nginx 配置:

/etc/nginx/站点可用/默认

    server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www;

        index index.php index.html index.htm index.nginx-debian.html;
        server_name xx.xxx.xx.xxx;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

注意:从那里开始,我显然不需要为其他网站设置任何其他配置文件......我可以通过它访问它们。这是正常的吗?在我以前的服务器上,我必须创建每个站点的配置文件。

不过,这里是其中之一

/etc/nginx/site-available/website1

server {
    listen 80;
    listen [::]:80;

    server_name _;

    root /var/www/website1;
    charset utf-8;
    index index.php index.html index.htm;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html { root /usr/share/nginx/html; }

    access_log /var/log/nginx/website-access.log combined;
    error_log /var/log/nginx/website-error.log error;

    if (!-e $request_filename) {
       # Don't use `$uri` here, see https://github.com/yandex/gixy/issues/77
        rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }

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

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

        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }

        location / {
            try_files $uri $uri/ /index.php?$args;
            #try_files $uri $uri/ /website1/index.php?q=$uri&$args;
            #try_files $uri $uri/ =404;
            #satisfy any;
            #allow all;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }
}

但正如我所说,无论有没有那个文件,我都可以访问 website1. 我可以仅使用默认配置文件访问它(或 /var/www/ 中的任何其他网站)这一事实对我来说似乎很奇怪......仍然存在永久链接问题。

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        #tcp_nopush on;
        #tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        index index.php index.html index.htm;

        # Upstream to abstract backend connection(s) for PHP.
        upstream php {
                #this should match value of "listen" directive in php-fpm pool
                server unix:/tmp/php-fpm.sock;
                #server 127.0.0.1:9000;
        }

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

我不是 nginx 专家,总是很难配置它,但总能弄明白。但在这里,我真的觉得卡住了。谢谢你的帮助!


更新

我设法让永久链接在 website1 上工作,但现在我不知道如何添加其他主机。

这是新文件。它也是唯一使用的配置文件。没有使用 /etc/nginx/site-available/website1 ...

/etc/nginx/站点可用/默认

server {
        listen 80 default_server;
        listen [::]:80 default_server;

          listen 443 ssl http2 default_server;
          listen [::]:443 ssl http2 default_server;
                ssl on;
                ssl_certificate_key /etc/ssl/private/nginx.key;
                ssl_certificate /etc/ssl/certs/nginx.pem;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                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:DHE-RSA-AES128-GCM-SHA256:DHE-$
                ssl_prefer_server_ciphers on;


        root /var/www/website1;
        index index.php index.html index.htm index.nginx-debian.html;

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

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
        }


location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    include fastcgi.conf;
    fastcgi_index index.php;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}


        location ~ /\.ht {
                deny all;
        }
}

标签: phpwordpressnginxpermalinks

解决方案


我终于找到了有用的东西。请告诉我这对最佳实践是否正确,如果不是,我应该怎么做?

这是有效的配置,我只使用一个文件来声明所有站点......

/etc/nginx/站点可用/默认

server {
    listen 80;
    listen [::]:80;
    root /var/www;
    index  index.php index.html index.htm;
    server_name  xx.xxx.xx.xxx;

    client_max_body_size 100M;

    location ~ \.php$ {
    include         snippets/fastcgi-php.conf;
    fastcgi_pass    unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location /website1 {
        root /var/www;
        try_files $uri /website1 /index.php?$args;
        }

     location /website2{
        root /var/www;
        try_files $uri /website2/index.php?$args;
        }

     location /website3{
        root /var/www;
        try_files $uri /website3/index.php?$args;
        }    
}

推荐阅读