How to configure alias directive in nginx?

I am trying to configure multiple webapps on my nginx web server, but I cannot get one Laravel application to work, which requires $ document_root to be installed in the laravel shared folder. I am currently trying to configure it using the alias directive, but for an unclear reason this does not work. Here is what I am trying to do.

# Default server configuration # server { listen 80; # SSL configuration # listen 443 ssl; error_log /var/log/nginx/error.log warn; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; set $root_path '/var/www/html'; root $root_path; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html index.php; server_name localhost; location /paperwork { alias /var/www/html/paperwork/frontend/public; try_files $uri $uri/; #location ~ \.php { # fastcgi_split_path_info ^(.+\.php)(.*)$; # fastcgi_pass unix:/var/run/php5-fpm.sock; # include /etc/nginx/fastcgi_params; # #fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; # #fastcgi_intercept_errors on; #} } #location @paperwork { # rewrite /paperwork/(.*)$ /paperwork/index.php/$1 last; #} location / { } location /wallabag { try_files $uri $uri/ /index.php; } location /laverna { try_files $uri/ /index.php; } # pass the PHP s to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { # With php5-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_split_path_info ^(.+\.php)(/.+)$; #try_files $uri $uri/ =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # deny access to .htaccess files, if Apache document root # concurs with nginx one location ~ /\.ht { deny all; } } 

To check the configuration of "alias", I put the test.php files in /var/www/html/paperwork/frontend/public/test.php and tried to access it through https: //IP/paperwork/test.php . I get a 404 error and nothing in the nginx error log. If I try https: //IP/paperwork/frontend/public/test.php in the browser, it will display the test.php file without errors. Nothing will change if I uncomment the try_files line in the php location.

If I copy test.php to / var / www / html / paperwork / test 2.php and access https: //IP/paperwork/test2.php , the file is displayed without errors, so I can see here that the alias is not works because test2.php does not exist in the public documentation directory.

I may have a different behavior if I uncomment the php location in the documentation location. At the same time, requests like https: //IP/paperwork/test.php do not display 404, but a blank screen.

I went through a lot of forums / questions related to this, but I could not get the working configuration for a simple task, for example, display test.php ...

Thanks!

+6
source share
1 answer

I have found a solution. It looks like the wrong request was sent for php files. When using an alias, it is recommended that you use $ request_filename instead of $ fastcgi_script_name.

Here is my location block:

 location /paperwork { alias /var/www/html/paperwork/frontend/public; #try_files $uri $uri/; location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; #fastcgi_intercept_errors on; } } 

This solved my problem for my test.php file, which now executes by reaching https: //IP/paperwork/test.php . This way the alias works, and php performs well. I still have a problem trying to reach "index.php" (this is my laravel application index). The file was found, but instead of downloading it, it loads. So when I reach https: //IP/paperwork/index.php , I get a loaded login file, which is index.php file. I get the same behavior if I try /paperwork/index.php/login or / paperwork / login.

+20
source

Source: https://habr.com/ru/post/982408/


All Articles