Nginx: Question mark in front controller rewrite rule - php

Currently I have this piece of code:
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
This works OK for the following:
example.com/foo redirects to index.php
However example.com/foo?bar doesn't work. How do you make it work?
FWIW: I don't experience this problem in Apache's mod_rewrite equivalent. Basically, I moved a site that works from Apache to Nginx. Now I experience this issue.
Edit:
To be clear here's what I indent to do:
example.com/foo
example.com/foo/bar/etc
example.com/foo?bar
example.com/foo?bar=quz
Should all serve index.php "silently" without changing the URL of the browser's address bar.

I just tested it with the following config, and I believe this does want you want:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/www/test;
index index.php index.html index.htm;
# Make site accessible from
server_name test.myhost.nl;
location / {
# First attempt to serve request as file, then as directory, then fall back to index.php
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri = 404;
# Fix for server variables that behave differently under nginx/php-fpm than typically expected
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with ngingx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Pass to upstream PHP-FPM; This must match whater you name your upstream connection
#fastcgi_pass phpfpm;
fastcgi_pass 127.0.0.1:9000;
}
}

Related

my_ip/phpmyadmin shows 404 Not Found on Nginx and after one tutorial instead loading my website it downloads a file

Trying to install phpMyAdmin on Nginx. Following all tutorials carefully, but when head my website my_IP/phpmyadmin I get 404 Not Found anyway.
And when I was loading my website by typing my IP address I got my website. But after following this tutorial instead of showing my website my browser downloads file "download". I deletted that code piece from tutorial, restarted Nginx, but browser still downloads a "download" file instead of heading my website. And when I type my_IP/phpmyadmin I still get 404 Not Found.
How I can get my settings back and run phpMyAdmin on my Nginx server?
Here is /etc/nginx/sites-available/default settings:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}
UPD:
Updated my PHP8.1, uncommented some important code pieces and added location /phpmyadmin code piece from that tutorial.
Looks like page is loading correctly, but my_IP/phpmyadmin still loads 404 Not Found.
Finally figured it out myself.
Actually in directory /etc/nginx/sites-available/ I already had two files, default and my_website.com. That piece of code I put into default. But now I moved this code to my_website.com file and it worked:
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
Thank you for attention!

Wordpress Nginx Permalink from plain to postname

I'm trying to change from plain http://sitename/?p=123 to http://sitename/postname with rewrite rule but it gives error 404 Not found. This is my nginx server block code:
server {
listen 80;
listen 443 ssl;
root /var/www/sitename/html;
index index.php index.html index.htm;
server_name sitename www.sitename;
client_max_body_size 10M;
# Certificates handled by CertBot
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
server_name sitename www.sitename;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ /index.php$is_args$args;
# rewrite ^ http://$server_name$request_uri permanent;
rewrite ^/(.*)$ http://$server_name/$1 permanent;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# root /data/www;
# index index.html index.htm;
}
}
I want to use rewrite so I redirect https of front-end to http and wp-admin and wp-login as https which is handled by Wordpress.
How can I fix it?
Created a separate similar server block with 443 listen. Included the cert lines and removed the location from 443 block.

nginx rewrite rule for trailing slash to load fpm/wordpress

Currently, i have nginx/php-fpm setup in a docker container and the wordpress folder mounted as a volume in the container. I want xyz.com/blog and xyz.com/blog/ both function exactly the same.
I have the following nginx configuration:
server {
listen 80 default_server;
server_name 0.0.0.0;
root /mnt/blog;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php last; #converted from .htaccess
}
}
location ~ .php$ {
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;
}
}
While hitting xyz.com/blog/ - the wordpress site gets loaded as expected but when i remove the trailing slash(/) - xyz.com/blog, i get 301 Moved Permanently error. How can i ensure that that both xyz.com/blog and xyz.com/blog/ behave correctly and load the wordpress site?
You want /blog to invoke /index.php without first redirecting to /blog/.
Based on your existing solution you could change the if (!-e ... to if (!-f .... See this document for details.
However, the same functionality can be achieved with:
location / {
try_files $uri /index.php;
}
See this document for details.

Symfony2, phpbrew, nginx, php 7.1 and File not found

I've been attempting to upgrade to php 7.1 using phpbrew, and elected to install it with nginx, as I read everywhere that it was simpler than Apache (not that simple, in my humble opinion).
As I was trying to run Symfony2 with nginx, I came across this doc page, which gives a basic config for Sf2 on nginx.
I managed to configure php-fpm to serve app_dev.php, and every file ending in .php correctly. However, as soon as I go to a different URL (/home for instance), the nginx config breaks and I get a File not found error in php-fpm.
How do I configure the nginx virtual host to allow for everything after app_dev.php or app.php to be rewritten (as it would with modrewrite on apache2)?
My nginx file for reference:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location /my-app {
index web/app_dev.php;
try_files $uri /web/app.php$is_args$args;
}
location /dist {
root /usr/share/nginx/html;
index depp/index.php;
try_files $uri /depp/index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REQUEST_URI $uri?$args;
}
}
You are missing a rewrite condition to catch-all the incoming requests and forward them to your front controller.
Try something like:
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location /my-app {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Symfony 2 app index
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to any other php files
location ~^.*\.php(/|$) {
deny all;
}
You current configuration is a more general configuration for any .php script but Symfony2 and framework in general only provide a catch-all front-controller.

October CMS /backend not found 404 with Nginx

I installed October CMS on my Nginx server, installed fine. Front end works with index.php and the style is correct. However backend does not. It just gives me a 404 page error. I've followed everything on the docs correctly, tried different sites-avaliable config files and they don't seem to work but give a 502, which I've checked the logs too and nothing in there.
This is my one3.com config file:
server {
listen 80;
root /storage/www/one3community.com;
index index.php index.html index.htm;
listen 443;
ssl on;
ssl_certificate /ssl_keys/one3community.com/public.pem;
ssl_certificate_key /ssl_keys/one3community.com/private.pem;
# Make site accessible from http://localhost/
server_name www.one3community.com;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Converted default apache .htaccess file to Nginx. Loaded into sites-available .conf file restarted nginx. Now up and running.
** Had to replace every break with last in nginx file otherwise causes file download **

Categories