server {
listen <...:...>;
server_name <...>;
root /var/www/html/myserver;
location /myproject {
try_files $uri /myproject/web/app.php$is_args$args;
}
location ~ ^/myproject/web/(app_dev|config)\.php(/|$) {
fastcgi_pass <...php-fpm...>;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ ^/myproject/web/app\.php(/|$) {
fastcgi_pass <...php-fpm...>;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
}
I can see my project when I trying to go myserver.com/myproject/web
I want access to my project at myserver.com/myproject.
But this url give 404 (in Symfony):
No route found for "GET /myproject/"
I added this rewrite rule in my configuration file but its not working:
rewrite ^/myproject(/|$)$ /myproject/web last;
Your config looks ok except for your root. The other paths should then change:
root /var/www/html/myserver/web;
location / {
...
}
location ~ ^/(app_dev|config)\.php(/|$) {
...
}
location ~ ^/app\.php(/|$) {
...
}
location ~ \.php$ {
...
You can see the NGINX config docs for reference.
If you want to use your existing config, then try this URL:
myserver.com/web
That might work.
Related
I am having an issue running /folder from a different path than the main website.
my nginx.conf for that section look like this:
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
}
location ~ /folder {
alias /srv/http/folder;
try_files $uri $uri/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi.conf;
}
}
location #folder {
rewrite /folder/(.*)$ /folder/index.php?/$1 last;
}
In the error.log I can see the following:
2020/06/03 09:05:26 [error] 25966#25966: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.21.2.46, server: example.com, request: "GET /folder/xxx_v6.15.11/Resources/images/redcaplogo.gif HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "example.com"
Any suggestion how to fix this?
Regex locations are matched from first to last, and the first found match processed by nginx. When you get a request for /folder/script.php, it is processed by the first location block. Try to swap this two locations. Additionaly, why are you do not include fastcgi_params in your second location block?
Update
I did some debugging, lets look at you code (assuming location block already swapped):
location ~ /folder {
alias /srv/http/folder;
try_files $uri $uri/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
}
location #folder {
rewrite /folder/(.*)$ /folder/index.php?/$1 last;
}
If we get a request /folder/some/path (and there is no some/path file or directory inside the folder), inside the nested location internal nginx variables would have following values:
$request_filename: empty string (there is no real file or folder /some/path inside th folder directory);
$uri: /folder/check.php;
$document_root: /srv/http/folder.
If we get a request /folder/some/path/script.php (and there is a real PHP script with this name), inside the nested location internal nginx variables would have following values:
$request_filename: /srv/http/folder/some/path/script.php;
$uri: /folder/some/path/check.php;
$document_root: /srv/http/folder.
Additionally, when you get a request for a static resource from your folder, for example /folder/some/path/static.css, the try_files $uri ... directive will search folder/some/path/static.css file in a /srv/http/folder directory, which leads to check the existence of /srv/http/folder/folder/some/path/static.css file.
One of possible solutions to get a file subpath inside the folder directory:
location ~ ^/folder(?<subpath>.*) {
alias /srv/http/folder;
try_files $subpath $subpath/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$subpath;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
}
location #folder {
rewrite ^/folder/(.*)$ /folder/index.php?/$1 last;
}
This could be simplified if your real folder directory name is the same as your /folder URI prefix:
location ~ ^/folder {
root /srv/http;
try_files $uri $uri/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
...
As nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
I have a problem with nginx and Symfony
I have a symfony app and it works when i follow the doc but i would like to make something like this
http://website.com/symfonyapp1 and http://website.com/symfonyapp2
here is my nginx config :
server {
server_name website.com;
location /symfonyapp1{
root /var/www/html/symfonyapp1/public;
# try to serve file directly, fallback to index.php
try_files $uri symfonyapp1 /index.php$is_args$args;
}
location ~ ^/symfonyapp1/public/index\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.2-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;
internal;
}
}
here is my /etc/hosts file
XXX.XXX.XX.XX website1.local symfonyapp1
XXX.XXX.XX.XX website2.local symfonyapp2
if someone can help me it can be very helpful
Thanks in advance
I have a Symfony 3.4 application running on nginx and a maintenance mode is implemented the following way in nginx:
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app)\.php(/|$) {
if (-f "/var/www/mysite.com/web/maintenance.html") {
return 503;
}
fastcgi_pass unix:/run/php/php7.0-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_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
I would like to exempt a certain location from maintenance mode, so that this location keeps being served by the Symfony application. So that requests to mysite.com/still/available/route are not redirected to the maintenance page.
How can that be done?
I found the answer, actually, it was quite obvious. One just needs to put the "if" into the root location block (/) and add another location block with the path that is supposed to remain accessible during maintenance mode. There, of course, must not appear that if-clause:
location / {
# try to serve file directly, fallback to app.php
if (-f "/var/www/mysite.com/web/maintenance.html") {
return 503;
}
try_files $uri /app.php$is_args$args;
}
location /still/available/route {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app)\.php(/|$) {
fastcgi_pass unix:/run/php/php7.0-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_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
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;
}
How do I make PHP-FPM rules play nicely with Nginx rewrite rules?
Current config file
server {
location / {
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?routestring=$1 break;
}
rewrite ^/(admincp/)$ /index.php?routestring=$1 break;
}
}
Change your location block to the following. Also try to avoid if statements, read about it (and more) here: http://wiki.nginx.org/Pitfalls
I've replaced the if (!-e ...) part with the #missing block in the config below.
server {
root /your/root/path
index index.php index.html index.htm;
server_name your.domain.com;
rewrite ^/(admincp/)$ /index.php?routestring=$1 break;
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$ {
# Move to the #missing part when the file doesn't exist
try_files $uri #missing;
# Fix for server variables that behave differently under nginx/$
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_nam$
# Pass to upstream PHP-FPM; This must match whater you name you$
#fastcgi_pass phpfpm;
fastcgi_pass 127.0.0.1:9000;
}
location #missing {
rewrite ^(.*)$ /index.php?routestring=$1 break;
}
}