简体   繁体   中英

Nginx + php5-fpm = 404 Error with alias location

I am an amateur front end web developer, and I recently bought a Ubuntu server to try to my hand at some backend development. I am trying to figure out how to serve a php file from an aliased location block using php5-fpm. I am getting a 404 - Page not found error. I have tried all of the proposed solutions I could find here with no luck. As I am still a beginner I would love a quick ELI5 as well and any pointers on the rest of my conf file, so I can learn something too. I should mention that the main root folder is running a flask app, and is the reason I am using an aliased location.

My virtual host:

Nginx conf file

server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;

root /var/www/example;
large_client_header_buffers 8 32k;
access_log /var/www/example/logs/access.log;
error_log /var/www/example/logs/error.log;


location / {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr; #$proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://app_test;
    proxy_redirect off;
}


 location /test_site {
     alias /var/www/test_site;
     index index.php index.html index.htm;
     location ~ .php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
                fastcgi_pass unix:127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
     }
  }

php5 www.conf file

[www]
...
user = www-data
group = www-data


listen = 127.0.0.1:9000
#listen = /tmp/php5-fpm.sock

listen.owner = www-data
listen.group = www-data
listen.mode = 0660
...

My fastcgi_params file is default. I have checked both the php and nginx logs and there are no errors. Any help would be much appreciated!

Getting alias to work with nested locations using fastcgi is complicated.

Assuming that you have not over simplified your configuration, the test_site location does not need to use alias :

location /test_site {
    root /var/www;
    index index.php index.html index.htm;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:127.0.0.1:9000;
        include fastcgi_params;
    }
}

This removes the alias directive, and solves the aliasing problem in the PHP block.

Note also: The regex on the location ~ \\.php$ block was wrong. The fastcgi_split_path_info and fastcgi_index directives are unnecessary.

The nginx directives are documented here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM