A
A
Anatoly2017-10-22 00:41:52
PHP
Anatoly, 2017-10-22 00:41:52

How to properly set up alias on Nginx so that php works?

I can't figure out how to set up alias on Nginx to make php work.
To make a simple alias work, it is enough to write:

location /aaa {
  alias /var/www/mysite;
}

How to make php work?
I write like this:
location /bb {
                alias /var/www/mysite;
}

location ~* ^/bb/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                alias /var/www/mysite/$1;
}

location ~ ^/bb/(.+\.php)$ {
                try_files $uri =404;
                alias /var/www/mysite/$1;
                fastcgi_pass backend;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
}

OS Debian 7.11 64bit, Nginx 1.12.1, PHP-FPM 7.1.10, opcache, memcached, http2, ssl,..
Without alias php works fine, today I experimented for half a day, read the whole Internet, the result is zero.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2017-10-22
@Tolly

Already almost gave up, and oppachki found the solution https://stackoverflow.com/questions/28490391/how-t...
The solution looks like this:
location /bb {
alias /var/www/mysite;
location ~ ^/bb/(.+\.php)$ {
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename ;
include fastcgi_params;
}
}

M
Mikhail Grigoriev, 2017-10-22
@Sleuthhound

If I understand your wish correctly, then it could be:

...
        location /aaaa {
                root /var/www/aaaa;
                index index.php index.html index.htm;
                location ~ (.+\.php)$ {
                       try_files $uri = 404;
                       root /var/www/aaaa/;
                       include /etc/nginx/fastcgi_params;
                       fastcgi_pass unix:/var/lib/php5-fpm/aaaa.sock;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                }
        }
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question