Wordpress Nginx别名无法正常工作

Wordpress Nginx别名无法正常工作,wordpress,nginx,alias,Wordpress,Nginx,Alias,我有一个Nginx vhost,看起来像: server { listen 80; root /var/www/mywebsite.com/www; # my index is not a wordpress index index.php index.html index.htm; charset UTF-8; server_name mywebsite.com; location ^/(al

我有一个Nginx vhost,看起来像:

server {
        listen   80;
        root /var/www/mywebsite.com/www; # my index is not a wordpress
        index index.php index.html index.htm;
        charset UTF-8;
        server_name mywebsite.com;

        location ^/(alias1|alias2)/(.*)$ { # my wordpress web site
                # i want 2 alias for the same wordpress
                alias /var/www/mywebsite.com/wordpress;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}
当我从中调用我的网站时

返回的php来自/var/www/mywebsite.com/www/index.php

如果我像/var/www/mywebsite.com/wordpress这样放置根,那么php从/var/www/mywebsite.com/wordpress/index.php返回,但静态返回404

调试日志的一部分:

2014/06/15 01:33:16 [debug] 3955#0: *3 using configuration "^/(alias1|alias2)"
2014/06/15 01:33:16 [debug] 3955#0: *3 http cl:-1 max:1048576
2014/06/15 01:33:16 [debug] 3955#0: *3 rewrite phase: 3
2014/06/15 01:33:16 [debug] 3955#0: *3 post rewrite phase: 4
2014/06/15 01:33:16 [debug] 3955#0: *3 generic phase: 5
2014/06/15 01:33:16 [debug] 3955#0: *3 generic phase: 6
2014/06/15 01:33:16 [debug] 3955#0: *3 generic phase: 7
2014/06/15 01:33:16 [debug] 3955#0: *3 access phase: 8
2014/06/15 01:33:16 [debug] 3955#0: *3 access phase: 9
2014/06/15 01:33:16 [debug] 3955#0: *3 access phase: 10
2014/06/15 01:33:16 [debug] 3955#0: *3 post access phase: 11
2014/06/15 01:33:16 [debug] 3955#0: *3 try files phase: 12
2014/06/15 01:33:16 [debug] 3955#0: *3 http script copy: "/var/www/mywebsite.com/public_html"
2014/06/15 01:33:16 [debug] 3955#0: *3 http script var: "/alias1/"
2014/06/15 01:33:16 [debug] 3955#0: *3 trying to use file: "/alias1/" "/var/www/mywebsite.com/public_html/alias1/"
2014/06/15 01:33:16 [debug] 3955#0: *3 http script var: "/alias1/"
2014/06/15 01:33:16 [debug] 3955#0: *3 trying to use dir: "/alias1/" "/var/www/mywebsite.com/public_html/alias1/"
2014/06/15 01:33:16 [debug] 3955#0: *3 http script copy: "/index.php?q="
2014/06/15 01:33:16 [debug] 3955#0: *3 http script var: "/alias1/"
2014/06/15 01:33:16 [debug] 3955#0: *3 http script copy: "&"
2014/06/15 01:33:16 [debug] 3955#0: *3 trying to use file: "/index.php?q=/alias1/&" "/var/www/mywebsite.com/public_html/index.php?q=/alias1/&"
2014/06/15 01:33:16 [debug] 3955#0: *3 internal redirect: "/index.php?q=/alias1/&"
2014/06/15 01:33:16 [debug] 3955#0: *3 rewrite phase: 1
2014/06/15 01:33:16 [debug] 3955#0: *3 test location: ~ "^/(alias1|alias2)"
2014/06/15 01:33:16 [debug] 3955#0: *3 test location: ~ "\.php$"
2014/06/15 01:33:16 [debug] 3955#0: *3 using configuration "\.php$"
2014/06/15 01:33:16 [debug] 3955#0: *3 http cl:-1 max:1048576
2014/06/15 01:33:16 [debug] 3955#0: *3 rewrite phase: 3
2014/06/15 01:33:16 [debug] 3955#0: *3 post rewrite phase: 4
2014/06/15 01:33:16 [debug] 3955#0: *3 generic phase: 5
2014/06/15 01:33:16 [debug] 3955#0: *3 generic phase: 6
2014/06/15 01:33:16 [debug] 3955#0: *3 generic phase: 7
2014/06/15 01:33:16 [debug] 3955#0: *3 access phase: 8
2014/06/15 01:33:16 [debug] 3955#0: *3 access phase: 9
2014/06/15 01:33:16 [debug] 3955#0: *3 access phase: 10
2014/06/15 01:33:16 [debug] 3955#0: *3 post access phase: 11
2014/06/15 01:33:16 [debug] 3955#0: *3 try files phase: 12
2014/06/15 01:33:16 [debug] 3955#0: *3 http script var: "/index.php"
2014/06/15 01:33:16 [debug] 3955#0: *3 trying to use file: "/index.php" "/var/www/mywebsite.com/www/index.php"
2014/06/15 01:33:16 [debug] 3955#0: *3 try file uri: "/index.php"
就像你看到的,一旦nginx找到了正确的道路 我忘记做什么了

因此,如果有人有解决方案,我希望standart php网站指向以下路径: /var/www/mywebsite/www

对于同一个域,我需要一个指向wordpress脚本路径的别名: /var/www/mywebsite/wordpress

所以,我在这里写了一个新的问题,更清楚:

您无法使用Nginx匹配多个位置,因此需要在位置块中使用别名包含PHP/fastcgi配置。如果这不是处理PHP文件的唯一地方,我建议将该配置放在不同的配置文件中,并使用include在多个地方使用它

php.conf

nginx.conf


你什么意思?虽然我删除了location~*\.js | css | png | jpg | jpeg | gif | ico${…},但结果是samei mean location~*^/alias1 | alias2/*$,因为它是一个正则表达式。这并不能解决问题。您的模式需要后面的斜杠。location~*^/alias1 | alias2{…}location~*/?:alias1 | alias2/*?${alias/var/www/mywebsite.com/wordpress$1;}我照你说的做了,但我的别名遇到了/var/www/mywebsite.com/www而不是/var/www/mywebsite.com/wordpress,因为我想看都不看模式-你需要使用~*作为正则表达式模式,您不希望在末尾使用.*$位置~*^/alias1 | alias2/Ok,但结果总是一样的:我的别名会运行到/var/www/mywebsite.com/www,而不是像我所希望的那样运行到/var/www/mywebsite.com/wordpress。您可能希望打开调试日志记录,这样您就可以知道是什么导致了别名,或者在这种情况下阻止了别名
location ~ \.php$ {
    # Zero-day exploit defense.
    # http://forum.nginx.org/read.php?2,88845,page=3
    # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
    # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
server {
    # ... your server config

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

    location ~* ^/(alias1|alias2)/ {
        alias /var/www/mywebsite.com/wordpress;
        try_files $uri $uri/ /index.php$is_args$args;
        include php.conf; 
    }

    include php.conf; # replaces your original config
}