This is the first time I use nginx with Symfony, and I used the configuration that is on the Symfony document. I have the basic configuration of the latest version of nginx.
However, on my url: 127.0.0.1:83/app_dev.php/fr/admin/organisations
I get the error in my nginx project_error.log file :
2017/06/01 09:16:14 [error] 20204#20204:
*1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,
client: 127.0.0.1, server: localhost,
request: "GET /app_dev.php/fr/admin/organisations/ HTTP/1.1",
upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "127.0.0.1:83",
referrer: "http://127.0.0.1:83/app_dev.php/fr/admin/organisations/1/edit"
My nginx server config (From Symfony documentation) :
server {
listen 83 default_server;
listen [::]:83 default_server;
server_name localhost;
root /var/www/SymfonySkeleton/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/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# 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;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
Any ideas ? I strongly assume that my configuration is not good
Thanks !
Remove the lines:
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
Though this may impact on the use of symlinks in your project. It will remove that error message though.
Please check the root path and the files under it .
Related
I'm trying to make a Nginx configuration where I can have Angular and Symfony both on the same domain but the Symfony on suburl.
The issue is that I'm getting all the time 404 Not Found for the Symfony Config.
My website has a default redirection for the Login and I'm being redirect to the url but after I'm getting the 404. (test.com/api/connect/azure) is the URL I'm being redirected to.
2022/01/10 15:16:15 [error] 23#23: *3 open()
"/var/www/angular/back/public/connect/azure" failed (2: No such file
or directory)
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/test_com.pem;
ssl_certificate_key /etc/nginx/ssl/test_com.pem.com.key;
server_name test.com;
root /var/www/angular/front/dist;
location /api {
alias /var/www/angular/back/public;
index index.php
try_files $uri /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass php8:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_intercept_errors on;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $request_filename;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
}
location / {
try_files $uri $uri/ /index.html;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
You have nested your location directives. I also see that you are listening on port 443 but there is no mention of which domain request nginx should respond to.
Consider using subdomains to host the Angular and Symfony applications as follows
angularapp.mydomain.com
symfonyapi.mydomain.com
You should separate the configuration for Angular and Symfony into seprate .conf files.
I still cannot understand what happening in my server
when i push my code throught the server in /public_html and wanna get the homepage of my application like this:
http:/www.example.com/web/
Server shows me the content of /web (the files and directory that exist inside /web)
and that's happen after i follow step's Command Line to deploy symfony 3 in that link:
https://symfony.com/doc/3.1/deployment.html
I Still cannot understand what's happening there!!
any help would be extremely appreciated!
1-)
yum install nginx
2-)service nginx start
3-) Add Nginx configuration into /etc/nginx/conf.d/project_name.conf
server{
set $web_host "domain_name";
set $web_root "web_directory";
server_name $web_host;
root $web_root;
location / {
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 127.0.0.1:9000;
# fastcgi_split_path_info ^(.+\.php)(/.*)$;
# include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
# fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
# fastcgi_param DOCUMENT_ROOT $realpath_root;
# }
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# 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;
}
4-) `chmod -R nginx:nginx /var/www/html/project``
5-) service nginx restartand service php-fpm restart
I'm developing a project using Symfony2, Nginx.
Project is located in my subdomain like developing_site.mysite.com.
I'd like to restrict access to this subdomain without authentication. Not only to dev and config files, but also to production.
So i added auth_basic component to nginx config file in location/ sector in nginx config that is recommended by symfony official web site.
As a result, before page loading server asks authentication and loads everything except for any files stores in /web directory like images, js, css and so on. As a result, there is all content processed by .php but without any style and dynamic functionality.
So how can i resolve this issue? What i'm doing wrong?
Nginx config looks like this:
server {
listen {MyServerIp};
server_name developing_site.mysite.com;
root /var/www/developing_site/web;
index index.php index.html index.htm;
location / {
try_files $uri /app.php$is_args$args;
auth_basic "Restricted Content";
auth_basic_user_file var/www/developing_site/.lock/.htpasswd;
}
# 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;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# 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;
}
I resolved the issue by myself..
Two mistakes:
syntactic mistake
incorrect place ofauth_basic block
The syntactic mistake is in var/www/developing_site/.lock/.htpasswd;. I used relative link instead of absolute. Correct form is /var/www/developing_site/.lock/.htpasswd; (sorry for that...)
When I've placed auth_basic block in location/ I've set authentication only to / location that in fact processes all /web requests... (/web requests wasn't processed because of 1-st mistake...)
Main symfony requests are processed by location ~ ^/(app_dev|config)\.php(/|$) block in nginx config file.
Solution: To restrict any requests to any files of developing_site.mysite.com without authentication, auth_basic block should be place before any location blocks.
So the correct nginx config should looks like this:
server {
listen MyServerIp;
server_name developing_site.mysite.com;
auth_basic "Unauthorized";
auth_basic_user_file /var/www/.lock/.htpasswd;
root /var/www/developing_site/web;
index index.php index.html index.htm;
location / {
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;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# 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;
}
Created a virtualhost for symfony application in local system
Here is the nginx config file
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_index app.php;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# Statics
location /(bundles|media) {
access_log off;
expires 30d;
try_files $uri #rewriteapp;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
On load of app in browser its throwing an error
502 Bad Gateway Error:No input file specified.
Error caught from error.log file:
FastCGI sent in stderr: "Unable to open primary script: /home/guest/symfony_demo/web/app.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: local.symfony, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "local.symfony"
Can anyone help me to configure symfony app to app_dev config file.
Any thoughts??
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
This line is the problem. If you remove it, your error disappears. You may then have Opcache problems due to Symfony using symlinks to the current project.
With $realpath_root$fastcgi_script_name, the web server looks at the "real" path for the PHP script based on the root definition in your server block. Nginx must have read permissions to the path for this to work. Your code is in /home/guest/, if nginx is running as "www-data" give it permissions to your directories, or run nginx as the "guest" user (ignoring the security implications of this).
Why don't you start with configuration from official documentation (http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html#nginx) and when that works, you can try to add your custom configuration (caching of sttaic files ...) ?
This should work:
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
location / {
try_files $uri /app_dev.php$is_args$args;
}
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 $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
This error happens when nginx is not able to find the php-fpm.sock file.
Can you make sure that the php-fpm.sock file is in the path as mentioned. I had to update fastcgi_pass like below
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
as my php-fpm.sock was there
I am deploying Laravel application on NGINX
I have a route specified in routes.php for handling non-blade php files and well as some of my blade php files
Route::any('/{pagephp}.php',function($pagephp) {
return View::make($pagephp); //This will handle .PHP as well as blades
});
But I get No Input File specified error whenever I try to access files such as terms.blade.php
Note that I do not get error when I access specified routes. for e.g. I have signin.blade.php for which I have
Route::get('/signin',function() {
return View::make('signin');
});
When looked in to error log I see
[error] 969#0: *91 FastCGI sent in stderr: "Unable to open primary script:
/var/www/xxxx/public/terms.php (No such file or directory)" while reading response
header from upstream, client: xxxxxxxx, server: xxxx, request: "GET /terms.php HTTP/1.1",
upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host:
As per the error, NGINX is trying to look for terms.php in public directory and not sending it to the route.php
Is there any way to fix this?
My NGINX config file is as follows
server {
# Port that the web server will listen on.
listen 80;
# Host that will serve this project.
server_name xxxx;
# Useful logs for debug.
access_log /var/www/xxxx/app/storage/logs/access.log;
error_log /var/www/xxxx/app/storage/logs/error.log;
rewrite_log on;
# The location of our projects public directory.
root /var/www/xxxx/public;
# Point index to the Laravel front controller.
index index.php;
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# PHP FPM configuration.
location ~* \.php$ {
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;
}
# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}
# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
expires 365d;
}
}
If these files does not belongs to laravel framework they should not be on the directory but if in case you want to test a particular php script you can put that in public folder and that will be accessible via yourdomain.com/script.php