Nginx with symfony2 File not found - symfony

I am configurating a virtual hosts with nginx. When I put my address, the page return me this error: File not found.
My configuration is the next:
server {
listen 80;
server_name vcarlos.lan;
root /home/tfc_dev/tfc/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;
}
}
EDIT #1
I have modified the code and I put the next:
server {
server_name vcarlos.lan;
root /home/sierra/tfc_dev/tfc/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fasserver {
server_name vcarlos.lan;
root /home/sierra/tfc_dev/tfc/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}tcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
Now it recognise Symfony2 but it return this error:
Oops! An Error Occurred
The server returned a "404 Not Found".
Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.*

Please replace:
rewrite ^/(.*)$ /app.php/$1;
with:
rewrite ^/(.*)$ /app.php?query_string;
UPD: Try this one:
server {
listen 0.0.0.0:80;
server_name vcarlos.lan;
root /home/tfc_dev/tfc/web;
index app.php;
try_files $uri $uri/ /app.php?$query_string;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}

Related

Override location directives in nginx not working

I want to override the permissions to /folder/script.php and have the following rules:
location ^~ /folder/script.php{
allow all;
} #shouldn't ^this one with ^~ override the others?
location ~ /folder/(.+)\.php$ {
deny all;
return 404;
allow 127.0.0.1;
}
location ~ ^/folder {
return 404;
}
location / {
# First attempt to serve request as file, then
# as directory, then trigger 404
try_files $uri $uri/ =404;
server_name_in_redirect off;
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#fastcgi_pass /tmp/php5-fpm.sock;
#fastcgi_pass /var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
# send bad requests
fastcgi_intercept_errors on;
include fastcgi_params;
}
but whenever I access admin.php I still get a 404 error and/or the script.php file is served to download, not interpreted. Could someone explain me why? Tyvm
The commands to execute a php script are:
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
fastcgi_intercept_errors on;
include fastcgi_params;
I'm not sure where you define $root_folder, normally $document_root is used. The above (or similar) code must appear in each and every location block that is expected to execute php scripts.
So your configuration should look something like this:
location / {
try_files $uri $uri/ =404;
server_name_in_redirect off;
}
location ^~ /folder { deny all; }
location = /folder/script.php {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
fastcgi_intercept_errors on;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
fastcgi_intercept_errors on;
include fastcgi_params;
}
I have taken the liberty of simplifying your configuration a little. It seems that only the file /folder/script.php is executable within the /folder hierarchy, so an exact match (location =) is used, and /folder is denied. The allow 127.0.0.1; does nothing as it comes after the deny all.
As you can see, the fastcgi_pass 127.0.0.1:9000; directive must appear in any location container that directly handles php code. I would put some or all of these directives into a separate file and use include to pull them in at each location.
The rest of the directives have been copied across from your question, but I don't know if they are required here.

How to configure Nginx to serve Wordpress in Symfony2 web directory

I have a Symfony2 project and I want to add a WordPress blog in /web/blog directory.
I need to know how can I configure the virtualhost to run both. My current configuration is:
server {
listen 80;
server_name my-project.local;
root /var/www/my-project/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/protox_error.log;
access_log /var/log/nginx/protox_access.log;
}
Can you help me?

Nginx locations configuration for two directions

I have a little experience with configuring nginx server and here is my trouble.
I am trying set correct locations. I have two directs: address.com and address.com/api.
For last direction(API) I have setted locations and it works fine. API is located in /var/www/project/api folder.
root /var/www/project;
index index.php;
server_name localhost;
location /api {
try_files /api/$uri $uri/ /api/index.php?$query_string;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^/api/(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 32k;
fastcgi_busy_buffers_size 64k;
fastcgi_buffers 4 32k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
Now I need implement root for address.com to /var/www/project/website. And here I have some troubles.
First thing, what I did I had written that:
location / {
alias /var/www/project/website/;
}
And then I tried to add many different variants and here is my last note.
I have put it inside location / {}
location ~ ^/(.+\.php)$ {
alias /var/www/project/website/;
include /etc/nginx/fastcgi.conf;
proxy_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
In /etc/nginx/fastcgi.conf file I have added
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
And I get all time 403 Forbidden or 404 Not found or in nginx errors log is written that, for example, /var/www/project/website/... is not found.
Has somebody experience with nginx configuring and can tell, how to set /website location correct?
Something like that:
server {
listen 80;
server_name localhost;
root /var/www/src/website;
index index.php index.html;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ =404;
}
location /test {
try_files $uri $uri/test.html =404;
}
location /api/ {
alias /var/www/src/api/;
try_files $uri $uri/ /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location /pmants {
root /var/www/src/;
index index.php index.html index.htm;
location ~ ^/pmants/(.+\.php)$ {
try_files $uri =404;
root /var/www/src/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/pmants/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /var/www/src/;
}
}
location ~* \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache off;
fastcgi_index index.php;
}
}

hosting symfony in a virtual directory on nginx

I'm trying to host a symfony app in a virtual folder and using url rewriting with nginx.
Following various examples I found, I'm stuck with something like that:
upstream phpfcgi {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 443;
server_name localhost;
root /realpath/Symfony/web/;
[ssl stuff]
# strip app.php/ prefix if it is present
rewrite ^/app_dev\.php/?(.*)$ /$1 permanent;
location /virtual{
alias /realpath/Symfony/web/;
index app_dev.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^/virtual/(.*)$ /app_dev.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass phpfcgi;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
}
}
If I remove the /virtual from the two first locations , it's working fine.
Should I set the SCRIPT_URI on the third location?
Thanks for your help
Here is the solution, see the added line:
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass phpfcgi;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Add this line:
fastcgi_param SCRIPT_NAME /virtual$fastcgi_script_name;
fastcgi_param HTTPS on;
}

ViMbAdmin and config nginx

I have a problem with the configuration of nginx. Structure directory in my server is:
/var/www/public_html is my page.
/var/www/pma - phpmyadmin
/var/www/vimbadmin - ViMbAdmin
My /etc/nginx/sites-available/default
server{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
access_log /var/www/log/access.log;
error_log /var/www/log/error.log;
root /var/www/public_html;
index index.php index.htm;
# Make site accessible from http://localhost/
server_name localhost;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /pma/ {
index index.php;
alias /var/www/phpmyadmin/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
}
}
location /vma/ {
index index.php;
alias /var/www/vimbadmin/public/;
try_files $uri $uri/ /index.php?$args;
index index.php;
location ~\.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#try_files $uri =404;
include fastcgi_params;
}
}
}
Now when I go: domian.com/vma/ redirect me to domian.com/vma/auth/login and I see:
File not found.
Phpmyadmin works.
I know that the configuration is bad... But where is mistake?
I did something like this
location ~ ^/vma {
alias /usr/local/vimbadmin/public;
location ~ ^/vma/(.*\.(js|css|gif|jpg|png|ico))$ {
alias /usr/local/vimbadmin/public/$1;
}
rewrite ^/vma(.*)$ /vma/index.php last;
location ~ ^/vma(.+\.php)$ {
alias /usr/local/vimbadmin/public$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
charset utf8;
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT /usr/local/vimbadmin/public;
}
}
it works, at least on my machine ;)
The answer of ir1keren did not work for me with ViMbAdmin V3.0.11.
Here is how I did it:
#
# ViMbAdmin 3.0.x Nginx configuration
# Directory where ViMbAdmin is installed
set $vimbadmin /var/www/vimbadmin;
location /vma {
alias $vimbadmin/public;
index index.php index.html index.htm;
location ~ ^/vma/(.*\.(js|css|gif|jpg|png|ico))$ {
alias $vimbadmin/public/$1;
}
rewrite ^/vma(.*)$ /mail/index.php last;
# Pass PHP scripts to FastCGI server
location ~ \.php$ {
#try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_split_path_info ^/vma/(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APPLICATION_ENV production;
fastcgi_index index.php;
fastcgi_pass php-backend;
fastcgi_intercept_errors on;
}
}

Resources