nginx try_files php file is not parsed - php

My project was split into three projects for historical reasons
This makes the nginx configuration file exceptionally difficult to handle, so much that I have tried many solutions and still haven't solved it perfectly.
Directory configuration:
/var/www/html/: project directory
/mnt/a.com/: another project directory
The third project uses a reverse proxy
The configuration file is as follows:
server {
listen 80;
listen [::]:80 default ipv6only=on;
server_name _;
root /var/www/html;
index index.html index.htm index.php;
sendfile off;
location / {
try_files $uri $uri/index.html $uri/index.htm $uri/index.php #mnt;
# try_files $uri $uri/ #mnt;
# This annotated code causes a 403 error to be returned when accessing an empty directory instead of redirecting to #mnt
}
location #mnt {
root /mnt/a.com;
try_files $uri $uri/index.html $uri/index.htm $uri/index.php #proxy;
}
location #proxy {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://a-proxy.com;
}
location ~ \.php$ {
try_files $uri #mntphp;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location #mntphp {
root /mnt/a.com;
try_files $uri #proxy;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
expires 2d;
}
error_log /var/log/a.com_error.log;
access_log /var/log/a.com.log;
}
But such a configuration will cause the php file to fail to parse. I know this is because try_files will redirect only the last parameter, but I don't know how to solve this

Related

Nginx configuration for Wordpress running along side Symfony

I have two applications:
a wordpress site at /var/www/html/wordpress
a symfony application at /var/www/html/symfony.
The wordpress application is running as the main domain (domain.com).
I want to achieve the following behavior:
a user visits URL domain.com/example1
nginx redirects to the Symfony route /example1.
With my current config nginx already redirects to the Symfony app.
It loads the wordpress site and its admin dashboard correctly.
Issue:
nginx returns the Symfony home page (/) instead of /example1.
The URLs domain.com/example1 and domain.com/example2 loads the Symfony homepage instead of its corresponding route created in the Symfony app.
My nginx configuration:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain.com;
server_tokens off;
root /var/www/html/wordpress;
index index.php index.html index.htm;
index index.html index.htm index.php;
client_max_body_size 500M;
# charset utf-8;
location / {
# CUSTOM
satisfy any;
charset utf-8;
allow 1.1.1.0/32;
deny all;
try_files $uri $uri/ /index.php?$query_string;
}
### start test
location ^~ /example1 {
satisfy any;
allow 1.1.1.0/32;
deny all;
index index.php;
alias /var/www/html/symfony/current/public/;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ^~ /example2 {
satisfy any;
allow 1.1.1.0/32;
deny all;
index index.php;
alias /var/www/html/symfony/current/public/;
try_files $uri $uri/ /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ ^((?!\/example1).)*$ { #this regex is to match anything but `/example1`
satisfy any;
allow 1.1.1.0/32;
deny all;
index index.php;
root /var/www/html/wordpress;
try_files $uri $uri/ /index.php?$request_uri;
#try_files $uri $uri/ /index.php?do=$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
### end test
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# CUSTOM
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ /\.(?!well-known).* {
deny all;
}
You're potentially overriding the fastcgi_* parameters with the defaults in the file fastcgi_params after setting them.
Instead of:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# the following line loads defaults from file `fastcgi_params`
include fastcgi_params;
Move the include directive after fastcgi_pass and fastcgi_split_path_info but before setting SCRIPT_FILENAME like this:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
include fastcgi_params;
# the following fastcgi_* parameters override the defaults in file `fastcgi_params`
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

How to open a phar.php file under nginx?

I'am running ddev under macos with apple silicon m1.
I'am trying to open a phar (Contao-Manager.phar.php) file in the browser but got a 404.
webroot is correctly set, because its is possible to open a test.php in same webroot in the browser.
Here Is My nginx-conf:
server {
listen 80 default_server;
listen 443 ssl default_server;
root /var/www/html/web;
ssl_certificate /etc/ssl/certs/master.crt;
ssl_certificate_key /etc/ssl/certs/master.key;
include /etc/nginx/monitoring.conf;
index index.php index.htm index.html;
sendfile off;
error_log /dev/stdout info;
access_log /var/log/nginx/access.log;
location / {
absolute_redirect off;
try_files $uri $uri/ /index.php?$query_string;
}
location #rewrite {
rewrite ^ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/run/php-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_read_timeout 10m;
fastcgi_param SERVER_NAME $host;
fastcgi_param HTTPS $fcgi_https;
}
location ~* /\.(?!well-known\/) {
deny all;
}
location ~* (?:\.(?:bak|conf|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
deny all;
}
include /etc/nginx/common.d/*.conf;
include /mnt/ddev_config/nginx/*.conf;
}
Try to change your default location to this (didn't check if works):
location / {
absolute_redirect off;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name $uri $uri/ /index.php?$query_string;
}
And check your access.log and error.log if it's not working.
The problem is somewhere between fastcgi_split_path_info and your location order (which location actually handles *.php request first).
It is also possible that your regular expression in fastcgi_split_path_info doesn't match Contao-Manager.phar.php (file has 2 extensions).

Nginx Try_files redirection losing query string

I am trying to setup a specific redirection to force redirection to index.php (for Laravel) for a specific subdirectory, bypassing existing index.html.
The url i want to catch looks like this app/kit//?email= . In each directory there's a index.html file (and for some business reasons it's hard to change this)
The redirection seems to work, but when i parse $_SERVER in index.php, i lose the query string.
My Nginx configuration look like this:
server {
server_name somedomain.com;
root /home/www/preprod/current/public/;
rewrite_log on;
access_log /var/log/nginx/preprod-access.log;
error_log /var/log/nginx/preprod-error.log notice;
location ~* /kit/(.*)/index\.html {
error_log /var/log/nginx/preprod-kit-error.log debug;
try_files /index.php?$query_string /dev/null;
}
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$query_string;
client_max_body_size 0;
autoindex off;
allow all;
}
location ~ \.php$ {
error_log /var/log/nginx/preprod-php-error.log debug;
#try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors off;
fastcgi_connect_timeout 300s;
fastcgi_read_timeout 15m;
fastcgi_send_timeout 600s;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
client_max_body_size 0;
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
All the application works fine, except this one.
Edit 1: snippets/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
#fastcgi_index index.php;
include fastcgi.conf;
I found a solution,i used this :
location ~* /kit/(.*)/(.*) {
error_log /var/log/nginx/preprod.adsconsole.com-kit-error.log debug;
rewrite /kit/(.*)/(.*) /index.php$is_args$args;
}
The regexp can obviously be better written, but if it can help someone !

Laravel 4 under Nginx Proxy Reverse

I'm trying to setting two Laravel apps...there is a main app and an API, which must run under a path
Main App URI: http://subdomain.domain.com
API App URI: http://subdomain.domain.com/api/
So, here the Nginx Config:
server {
listen 80;
server_name subdomain.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name subdomain.domain.com;
ssl on;
ssl_certificate ...;
ssl_certificate_key ...;
root /usr/share/nginx/html/main_project/current/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /api {
proxy_pass http://127.0.0.1:81;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Cache-Control public;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=none";
fastcgi_param HTTPS on;
}
}
server {
listen 81;
root /usr/share/nginx/html/api_project/current/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The issue is: routing under API app is not working. Anyone can help me?
You seem to be missing the path split directive in the second server fastcgi block.
Try modifying it in the following way :
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Symfony2 nginx issue - all pages 404 not found

I've got problem with my project in symfony2 when i'm trying to run this on nginx.
www.domain/ works fine
www.domain/app_dev.php ,"An error occurred while loading the web debug toolbar (404: Not Found).Do you want to open the profiler?"
www.domain/app.php/someurl/ - 404 not found
Is it server configuration issue or should I change my .htaccess in some way to make it possible to run with nginx?
You can check my dev config (with PHP-FPM):
server {
listen 80;
server_name dev.example.com;
root /var/www/web;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log error;
index app.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /app.php/$1;
}
location ~ \.php(/|$) {
# try_files $uri =404;
fastcgi_index app.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
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;
fastcgi_buffer_size 1280k;
fastcgi_buffers 4 2560k;
fastcgi_busy_buffers_size 2560k;
}
location ~ /\.ht {
deny all;
}
}

Categories