Nginx别名不能正常工作

时间:2014-06-14 17:45:31

标签: 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 ^/(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;
        }
}

当我从http://mywebsite.com/alias1

致电我的网站时

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

如果我将 root 设置为 /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找到正确的路径,但在他之后 我忘记做了什么?

所以,如果有人有解决方案,我想要一个标准的php网站定位到一个路径: 的/ var / WWW / mywebsite /万维网

对于同一个域我想要一个定位到wordpress脚本路径的别名: 的/ var / WWW / mywebsite / WordPress的

所以,我在这里写一个新问题: Nginx vhost website + wordpress alias in subfolder

1 个答案:

答案 0 :(得分:0)

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

<强> php.conf

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;
}

<强> nginx.conf

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
}