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

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.

Related

How to add rewrite on existing Nginx config file

I have an existing Nginx config file that I've configured to work with a docker container that runs php. Below is the nginx config file:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Now I wanted to test a certain project in the nginx configuration, the project requires that I use its own configuration of nginx
# nginx configuration
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?path=$1;
}
}
I am getting difficulties on adding the new configurations onto the already existing Nginx file that connects to the PHP container. I need some help on how I can add the new configurations without compromising the old Nginx file because the routes wont be redirected to the php container if I use the Nginx file that comes with the project.

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

Nginx config for symfony 2

I want to configure my symfony 2 application so that all /web/app.php are omited, unless that's /web/app_dev.php, then I want to enter the development environment.
The problem is that I always get 502 bad gateway error. What's wrong?
This is my current configuration:
server {
listen 80;
# Server name being used (exact name, wildcards or regular expression)
server_name domain.com;
client_max_body_size 20M;
# Document root, make sure this points to your Symfony2 /web directory
root /var/www/domain.com/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
# Logging
error_log /var/log/nginx/domain.com.error.log;
access_log /var/log/nginx/domain.com.access.log;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
fastcgi_index index.php;
}
}

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