nginx rewrite rule for trailing slash to load fpm/wordpress - php

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.

Related

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 **

Permalinks in Wordpress not working with Laravel + Nginx

I have a site developed with Laravel on my main route say www.example.com/. I have configured it properly with Nginx and php-fpm. My config is below.
Then I added a blog in route /blog (www.example.com/blog/) and configured it with Nginx alias.
Now the problem is that Permalinks in Wordpress are not working. Nginx redirects to Laravel's 404 page.
For example when user enters some URL like this: example.com/blog/about, Laravel's 404 page shows up which is weird.
How can I fix this? How can I config Nginx? What's Wrong?
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html/;
location /blog {
try_files $uri $uri/ /index.php?$args;
alias /usr/share/nginx/blog/;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx$fastcgi_script_name;
}
}
location / {
root /usr/share/nginx/main_site;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
You do not need to use alias when the location matches the end of the alias path. See this document.
The try_files in location /blog needs to default to the WordPress router (/blog/index.php) and not the Laravel router (/index.php).
Try:
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
root /usr/share/nginx;
...
location ~ \.php$ {
...
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Suppose you are running laravel on your server and you want to use wordpress as a blog. and you installed wordpress in blogs named folder. Then you have to do changes in 3 places
1) laravel .htaccess file
2) blogs folder .htaccess file
3) nginx configuration file
Laravel.htaccess file changes add this line
RewriteCond %{REQUEST_URI} !^/blogs/
blogs folder .htaccess file add this code or modify
RewriteEngine On
RewriteBase /blogs/ # <==== CHANGED LINE!!
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blogs/index.php [L] # <==== CHANGED LINE!!!
in nginx configuration file try modify below code in both port 80 and port 443 code block
location /blogs {
try_files $uri $uri/ /blogs/index.php?$args;
set $base /var/www/html;
root $base/public;
location ~ \.php$ {
fastcgi..
}
}

Laravel and WordPress nginx config

I would like to have WordPress installed in my Laravel 4 project at /public/cms, I don't need WordPress pages to be accessible through the browser but I do need to Post to WordPress routes to update WP content (forms) from within laravel.
I have tried updating my vhosts file to match with the answer here: How to install WordPress alongside Laravel on Nginx with pretty permalinks (SEO-friendly URLs)?
But this is causing a redirect loop.
here is my nginx vhost file:
server {
listen 80;
server_name laravel.mylifemysmile.org;
root /var/www/mylifemysmile/public;
# Point index to the Laravel front controller.
index index.php;
location /cms/ {
try_files $uri $uri/ #wordpress;
}
location #wordpress {
rewrite /cms/ /cms/index.php;
}
location ^/cms/index.php(/.*)?$ {
fastcgi_split_path_info ^(/cms/index.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Update
Here's the new nginx vhost (server block) file:
server {
listen 80;
server_name laravel.mylifemysmile.org;
root /var/www/mylifemysmile/public;
# Point index to the Laravel front controller.
index index.php;
location / {
try_files $uri $uri/ /index.php$query_string #laravel;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
set $php_root /var/www/mylifemysmile/public;
if ($request_uri ~ /cms/) {
# ok, this line is a bit confusing, be aware
# that path to /cms/ is already in the request
# so adding a trailing /cms here will
# give a "no input file" message
set $php_root /var/www;
}
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
}
# now wordpress, remember this lives in a sibling directory of the main app
location ~ /cms/ {
try_files $uri $uri/ /index.php$args;
# again, this might look a bit weird,
# but remember that root directive doesn't drop
# the request prefix, so /cms is appended at the end
root /var/www;
if (!-d $request_filename)
{
rewrite ^/(.*)/$ /$1 permanent;
}
}
}
I moved wordpress out of laravel/public and into its own folder on the same level as laravel. I can now access WordPress content (including images) in the laravel site, but still can't access the wordpress dashboard.

Nginx: Question mark in front controller rewrite rule

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

Categories