Nginx doesn't find Symfony2 routes - php

I try to run a simple Symfony2 application on a virtual machine. I have Nginx on this VM (generated with PuPHPet and Vagrant), but there's a problem.
When I try to access to the demo route /demo (local.dev/Symfony/web/app_dev.php/demo), Nginx force me to download a file. It seems it works the same way when I specify a random route.
I don't know how to solve this problem, I searched while 2 days for a specific configuration of Nginx but I found nothing. Here is my config file for this Symfony app :
server {
server_name test.dev;
root /var/www/local.dev/Symfony/web;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_log /var/log/nginx/sf-error.log;
access_log /var/log/nginx/sf-access.log;
}
Thanks for your help.

Related

Nginx php rewrite for symfony isn't allowing to access standard php files

Sorry if this is a dumb question, i am quite new to using nginx after switching from apache. I have a symfony app running on nginx and it's working using containers and cgi pass. I am able to access symfony routes on it without an issue, but i also have a single php file in the public folder that needs to be accessed, but it's giving me 404.
I think it's related to the nginx rules but i'm not sure how to get around it
events {
worker_connections 1024;
}
http {
server {
listen 80;
root /usr/share/nginx/html/public;
server_name api.gofollow.vip;
location / {
try_files $uri /index.php$is_args$args;
#try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
internal;
}
# deny access to apache .htaccess
location ~ /\.ht {
deny all;
}
error_log /usr/share/nginx/logs/error.log;
access_log /usr/share/nginx/logs/access.log;
}
}
So i need the symfony routes like example.com/page/hello to work, which they do, but i also need to be able to access example.com/tester.php
It can block any other php file, but "tester.php" needs to be accessible.

Laravel app with separate php-fpm and nginx servers

Is it possible to run a Laravel app on two separated server, one with NGINX, another with PHP-FPM?
I wanted to create an upstream of PHP-FPM servers for load balancing. I've tried some NGINX configurations but it seems that both NGINX and PHP-FPM needs Laravel app files.
Note: I have separate server for static files.
[user] -request-> [nginx without laravel files] -> [php-fpm upstream with laravel files]
UPDATE
upstream php_pool {
server 192.168.1.1:9000;
}
server {
listen 80;
server_name www.example.com;
index index.php;
access_log /var/log/nginx/q_access.log;
error_log /var/log/nginx/q_error.log info;
location / {
try_files $uri $uri/index.php /index.php;
}
location ~ \.php$ {
index index.php;
fastcgi_pass php_pool;
fastcgi_index index.php;
include fastcgi_params;
}
}
I don't know what should i set for root directory cause there isn't any Laravel app files in that server. And how config \.php$ location properly?
We found solution for our scenario. PHP location should be like this:
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php_pool;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/app/public$fastcgi_script_name;
}
With this change (SCRIPT_FILENAME) there isn't need for Laravel app files to be on the NGINX server cause all PHP requests will proxied to PHP-FPM servers.

Nginx + PHP on docker serving static files

I have two containers: nginx and php-fpm. The files are stored on the php-fpm container. There is no problem with serving PHP files, but I need to serve static files as well, and I have a problem with the location configuration.
No idea how to configure it correctly.
location /static/ {
fastcgi_pass api:9000;
}
or I tried:
location /vendor/ {
root api:9000/public/vendor/;
}
This is the config file:
server {
listen 80 default_server;
root /app/api/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /static/ {
fastcgi_pass api:9000;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_pass api:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 3600;
}
}
Thank you!
As #slauth mentioned, there is no way to access files to another container.
I decided to serve static images using PHP. Here is a good tutorial: https://grahamlikesto.codes/slim-php-how-to-serve-static-files-with-slim-php/
This is not the best solution. My app is a small app with little traffic, and serving assets is not a big deal, but you should change your architecture if you run this issue.

NGINX not executing php files

First at all I am new here and new to unix. My previous experience was solely with cPanel, Plesk etc. So please forgive me if there are some mistakes in my approach here.
I have a Centos (release 7.3.1611) VPS with Nginx (1.10.2) and PHP (7.0.17).
I followed some guides to get PHP running and followed some answers given here to similar problems as well to no avail.
Here is my configuration:
www.conf:
listen = /run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
I changed permission and ownership on php-fpm.sock.
nginx.conf:
include /etc/nginx/default.d/*.conf;
nginx/default.d/default.conf:
index index.php index.html index.htm;
server_name _;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Finally I have created a info.php file. When executing it I am getting a nginx error: The page you are looking for is temporarily unavailable. Please try again later.
If disabling the nginx error page the browser is asking me if I want to download the file info.php.
Thank you for helping me out!!!
I initially encountered similar issues but fixed by making sure the path to 'include' and 'fastcgi_pass' were valid. You can verify if something is available at these paths by navigating to those directories.
# PHP 7
# cgi.fix_pathinfo=0 in php.ini
# case-sensitive regex
location ~ \.php$ {
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
try_files $uri $uri/ =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Hope this helps.
Cheers,
Ian

Laravel 5 - Nginx/PHP config issue

I'm having an issue where when I go to the /public directory it shows the Laravel app as normal, but navigating away to any other page results in it saying
No input file specified.
I am using an Nginx server with PHP 5.5.9 FPM.
I've scoured google for the last 4 hours or so, looking at every tutorial and stackoverflow page for rewriting issues in Laravel however they all yield the same result.
I've even set all the files and folders to 777 so I could see if it was some sort of permissions issue. I've checked the Laravel config and it's all set, I've no idea what is wrong.
Can anyone point me in the right direction?
The last config I tried is below:
server {
listen 80 default_server;
root /usr/share/sites/base;
index index.php
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
I have also tried many others such as:
server {
listen 80;
server_name domain.com;
root /usr/share/sites/base;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~* \.php$ {
# Server PHP config.
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
The error "No input files specified" will nearly always be related to the fact that the wrong path was sent to php.
Looking at your 'last config tried' I can see that fastcgi_param SCRIPT_FILENAMEis not defined in your php location. You should first begin by defining it in the location :
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
}
Furthermore you say that you can reach the app so this means that index.php is working but not when you change page. So the problem should also come from /index.php?$args. Indeed, using this line if I try to reach yourserver.com/test and if 'test' is not a file in your root path nginx will then try request /index.php? (I had this probem). You should try only with /index.php.
EDIT : The solution was that root directive should point to the Laravel public folder, in that case /usr/share/sites/base/public.

Categories