Reputation: 924
I would like to nest several dedicated routes within a site to specific directories which might not have the same name. I can't figure out how to do a rewrite of the path it uses for the try_files
.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/default;
index index.html;
server_name _;
if ($bad_referer) {
return 444;
}
location / {
try_files $uri $uri/ =404;
}
location /postfixadmin/ {
access_log /var/log/nginx/postfixadmin/access.log;
error_log /var/log/nginx/postfixadmin/error.log;
root /var/www/postfixadmin/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
location ~ \.php$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
location /email/ {
#access_log /var/log/nginx/roundcube/access.log;
#error_log /var/log/nginx/roundcube/error.log;
root /var/www/roundcube/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
location ~ \.php$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
}
When I navigate to www.site.com/email
, I receive a 404
, and I'm assuming that is because it's looking for /var/www/roundcube/email/index.php
, which doesn't exist. What do I need to do to rewrite the file path before the try_files
?
Upvotes: 1
Views: 5521
Reputation: 924
I figured out the solution, and it turns out to be fairly simple. Using alias
, rather than root
uses only the section of the string after the portion matching the location
, so I was looking in the right directory. The other problem was that PHP wasn't being passed the correct script name so it was still looking in the wrong place. Solution was to pass in fastcgi_param SCRIPT_FILENAME $request_filename;
. I was also able to get rid of the try_files
section, though I'm not 100% certain why.
Here is the working solutions:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/default;
index index.html;
server_name _;
if ($bad_referer) {
return 444;
}
try_files $uri $uri/ =404;
location /postfixadmin {
alias /var/www/postfixadmin/;
index index.php index.html index.htm;
location ~ /postfixadmin/.+\.php$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
location /email {
alias /var/www/roundcube/;
index index.php index.html index.htm;
location ~ /email/.+\.php$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
}
Upvotes: 1