0
votes

I have a Nginx vhost look like:

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

When i call my web site from http://mywebsite.com/alias1

The php returned is from /var/www/mywebsite.com/www/index.php

if i put my root like /var/www/mywebsite.com/wordpress so the php is returned from /var/www/mywebsite.com/wordpress/index.php but the static return 404.

A part of my debug log:

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"

Like you see, once nginx find the right path but after he What did I forget to do?

So if anybody have the solution i want a standart php website to target to a path: /var/www/mywebsite/www

And for the same domain i want an alias who target to a wordpress script path: /var/www/mywebsite/wordpress

So, I write a new question, more clearly here: Nginx vhost website + wordpress alias in subfolder

1
what did you mean? Althouth i remove location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { ... } the result is the sameRaKoDev
i mean location ~* ^/(alias1|alias2)/(.*)$, since it's a regular expression.Deadooshka
That not solve the problem.RaKoDev
your pattern expect the trailing slash. location ~* ^/(alias1|alias2) { ... }Deadooshka
location ~* ^/(?:alias1|alias2)(/.*)?$ { alias /var/www/mywebsite.com/wordpress$1; } nginx.org/en/docs/http/ngx_http_core_module.html#aliasDeadooshka

1 Answers

0
votes

You cannot match multiple locations with Nginx, so you need to include your PHP/fastcgi config inside the location block with the alias. If this isn't the only place you want to process PHP files, I would recommend putting that config into a different config file and use include to use it in multiple places.

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
}