Nginx nested location with route rewrite - php

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?

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

Related

php nginx rewrite urls to index.php with

I've been trying to get this to work for a while now, but I'm failing manifold.
I have the following configuration:
server {
listen 8081;
server_name name.of.server.en;
root /path/to/api;
index index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/([A-Za-z0-9]+)/$ /index.php?data=$1? last;
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ /index.php?data=$1&id=$2? last;
return 404;
}
}
nginx -t says that everything is ok. But as soon as I call the URL I always get a 404 Not Found.
I have no idea what I am doing wrong. Probably something completely banal, but I can't figure it out.
I am almost at the despair.
Here is what I am running for PHP7.4 for local testing. Might be helpful to you.
server {
listen 80;
root /var/www/html/public;
index index.html index.php;
server_name fancyproject;
charset utf-8;
location / {
root /var/www/html/vue/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api/ {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
add_header "Access-Control-Allow-Origin" *;
add_header "Access-Control-Allow-Methods" "GET,POST,OPTIONS,HEAD,PUT,DELETE,PATCH";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Nonce, DNT, X-CustomHeader";
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /.ht {
deny all;
}
}
The order is important at this point.
This is how it should work:
server {
listen 8080;
server_name the.server.name.org;
root /path/to/api;
index index.php;
location / {
rewrite ^/(.*)$ /index.php?data=$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}

PHP files not automatically loaded in nginx

When I try to access my site I have to manually type index.php at the end of the url. I'm trying to have automatically load my index.php file when accessing the site. My nginx config file looks like this.
server {
listen 80;
root /var/www/html/myapp/public;
# Add index.php to the list if you are using PHP
server_name _;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location / {
index index.html index.htm index.php;
# try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ = 404;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
for laravel Apps, you need to have the below in your nginx conf
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index /index.php;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

rewrite prefix url in nginx location

My nginx config file like this:
server {
listen 80;
listen 443 ssl;
server_name XXX.com;
error_log /log/nginx/xxx.com_error.log;
access_log /log/nginx/xxx.com_access.log main;
root /data/www/;
index index.php index.html index.htm;
location ~ \.php$ {
add_header X-Frame-Options SAMEORIGIN;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
We need to config the nginx to satisfy following:
1、If url does not has prefix "/api/mobile/index.php",and the request's port is 80, redirect it to https
2、If url has prefix "/api/mobile/index.php",just go on
So I add content in the config file:
location ~ ^(?!/api/mobile/index\.php).*$ {
if ($server_port = "80") {
return 301 https://$server_name$request_uri;
}
rewrite /* $server_name$reqeust_uri last;
}
Now the config file content is :
server {
listen 80;
listen 443 ssl;
server_name XXX.com;
error_log /log/nginx/xxx.com_error.log;
access_log /log/nginx/xxx.com_access.log main;
root /data/www/;
index index.php index.html index.htm;
location ~ ^(?!/api/mobile/index\.php).*$ {
if ($server_port = "80") {
return 301 https://$server_name$request_uri;
}
rewrite /* $server_name$reqeust_uri last;
}
location ~ \.php$ {
add_header X-Frame-Options SAMEORIGIN;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Than the request match the first location, will not match the other location.
That means these request couldn't go through the php cgi.
Is there anyone who knows how to solve the problem?
Nginx matches only one location. Move config to first location too.
location ~ ^(?!/api/mobile/index\.php).*$ {
if ($server_port = "80") {
return 301 https://$server_name$request_uri;
}
add_header X-Frame-Options SAMEORIGIN;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
add_header X-Frame-Options SAMEORIGIN;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
There's the option to use two separated server context, and didn't use if statement (read why here: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/).
The configuration could be:
server {
listen 80;
server_name XXX.com;
error_log /log/nginx/xxx.com_error.log;
access_log /log/nginx/xxx.com_access.log;
root /data/www;
index index.php index.html index.htm;
location /api/mobile/index.php {
rewrite ^(.*)$ https://$host$1 redirect;
}
location ~ \.php$ {
add_header X-Frame-Options SAMEORIGIN;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_param;
}
}
server {
listen 443 ssl http2;
server_name XXX.com;
error_log /log/nginx/xxx.com_ssl_error.log;
access_log /log/nginx/xxx.com_ssl_access.log;
root /data/www;
index index.php index.html index.htm;
location ~ \.php$ {
add_header X-Frame-Options SAMEORIGIN;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_param;
}
}

Header is not being added to specific path NGINX, Laravel

Trying to add a no index header to a specific path (/path) but it doesn't show up. When I add it in with the other add_header lines, it shows just fine but not when put into location /path {} or even location / {}. I've read a bunch of answers on the forum and haven't been able to figure out the issue. Anyone know or see what I'm doing wrong? Nginx version is 1.13.3
Here's my nginx file:
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name mydomain.com;
root /home/forge/mydomain.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "origin";
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /path {
add_header X-Robots-Tag "noindex" always;
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}

Running multiple projects in nginx using php-fpm creating session for each http request from one application to another

I have 2 independent application which is built on Zend framework with php-fpm and running on Nginx on the same server. When I' m calling API from one application to another application. Session are creating for each request under following folder "/var/lib/php/session". The above function is called only one time when the user is logged in.
if (session_status() == PHP_SESSION_NONE || session_status() !== PHP_SESSION_ACTIVE)
{
session_start();
}
The NGINX configuration is below:
First Application:
server
{
listen 80 default;
listen 443 ssl;
server_name $hostname;
client_max_body_size 16384M;
location /
{
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Content-Type,accept,x-wsse,origin';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
root /../first_application/public;
index index.php index.phtml index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$
{
root /../first_application/public;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_read_timeout 300;
include fastcgi_params;
}
location ~ /\.ht
{
deny all;
}
}
Second Application:
server
{
listen 8118 default;
listen 8119 ssl;
server_name $hostname:8119;
client_max_body_size 16384M;
location /
{
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Content-Type,accept,x-wsse,origin';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
root /../second_application/public;
index index.php index.phtml index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$
{
root /../second_application/public;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_read_timeout 300;
include fastcgi_params;
}
location ~ /\.ht
{
deny all;
}
}

Categories