I would like to serve 2 PHP(Symfony) applications under the same domain, with a URL prefix for one of the apps.
So www.mydomain.com/ should serve the first app while www.mydomain.com/secondapp should serve the second one.
I ended up with this config which doesn't work :
server {
server_name mydomain.com www.mydomain.com;
location / {
root /var/www/first-app/web;
try_files $uri /app.php$is_args$args;
}
location /secondapp {
root /var/www/second-app/web;
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|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;
}
}
When I put the root directive for the first app under the last location, it works for the first app but the second can't be served.
Should I create a second server directive and use sub-domain rewriting instead?
Thanks!
You should use alias instead of root in your locations. If you use root, the path that is matched in the location is added to the root path.
So I finally found the solution: my nginx conf:
server {
listen 80;
server_name www.mydomain.com;
root /var/www/first-app/web;
location ~ ^/secondapp(/.*)$ {
alias /var/www/second-app/web;
try_files $1 #app;
}
location #app {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/second-app/web/app.php;
fastcgi_param SCRIPT_NAME /secondapp/app.php;
fastcgi_param REQUEST_URI /secondapp$1;
}
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|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;
}
error_log /var/log/nginx/app_error.log;
access_log /var/log/nginx/app_access.log;
}
Related
I would like to run symfony app in subdirectory like example.com/api. What is wrong with my config? I recieve 404 without other errors
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
location /api {
alias /var/www/dev/symfony-server/public/;
try_files $uri index.php$is_args$args;
fastcgi_pass unix:/var/run/php/php7.3-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;
}
}
You are missing your root directory for your nginx to look into.
Try something like the following:
server {
listen 127.0.0.1:80;
server_name dev.blog.local blog.local www.blog.local;
root /Volumes/git/blog/public;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/(index)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ \.php$ {
return 404;
}
}
lets say I've a path like:
/var/www/myside/
that path contains two folders... let's say
/static and /manage
I'd like to configure nginx to have an access to:
/static folder on / (eg. http://example.org/)
this folder has some .html files.
/manage folder on /manage (eg. http://example.org/manage) in this case this folder contains Slim's PHP framework code - that means the index.php file is in public subfolder (eg. /var/www/mysite/manage/public/index.php)
I've tried a lot of combinations such as
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite;
location /manage {
root $uri/manage/public;
try_files $uri /index.php$is_args$args;
}
location / {
root $uri/static/;
index index.html;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
The / works correctly anyway manage doesn't. Am I doing something wrong? Does anybody know what should I change?
Matthew.
To access a path like /var/www/mysite/manage/public with a URI like /manage, you will need to use alias rather than root. See this document for details.
I am assuming that you need to run PHP from both roots, in which case you will need two location ~ \.php blocks, see example below. If you have no PHP within /var/www/mysite/static, you can delete the unused location block.
For example:
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite/static;
index index.html;
location / {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /manage {
alias /var/www/mysite/manage/public;
index index.php;
if (!-e $request_filename) { rewrite ^ /manage/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
The ^~ modifier causes the prefix location to take precedence over regular expression locations at the same level. See this document for details.
The alias and try_files directives are not together due to this long standing bug.
Be aware of this caution in the use of the if directive.
Here is my nginx's content.
My current access url is http://localhost/lampi/, and I am receiving response 403 forbidden.
My server's documentroot is /Library/WebServer/Documents/.
When I access http://localhost/, it shows ok. I can also see the index.html page's content.
I don't know what the matter is. I have checked top 10 pages in stackoverflow.
server {
server_name localhost;
access_log /var/log/nginx/nginx.host.access.log main;
root /Library/WebServer/Documents/;
location / {
#root html;
index index.html index.htm index.php;
}
location /lampi {
#autoindex on;
if (!-e $request_filename){
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
}
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/Documents/lampi/$fastcgi_script_name;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
#location /images/ {
# root /usr/local/var/www;
#}
}
There are three potential problems with your configuration file.
The use of if (!-e $request_filename) is causing problems because it checks for the existence of directories, and you should probably be using try_files anyway (see this document for details):
location /lampi {
try_files $uri $uri/ #lampi;
}
location #lampi {
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
The value of SCRIPT_FILENAME adds an extra /lampi into the pathname. Use either of (both evaluate to the same value in this case):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
For example:
location ~ \.php$ {
try_files $uri =404;
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The location ~ [^/]\.php(/|$) block is confusing. Normally it is adequate to use either location ~ \.php$ or something like location ~ [^/]\.php(/|$) depending on whether or not your application uses PATH_INFO. Delete the block you are not using. See this document for details.
I am working on a laravel V5.1.11 site which is hosted on AWS EC2 ubuntu with ngnix server. I successfully setup the site but my inner page are not working.
Config is:
server {
listen 82;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 83;
server_name www.example.com;
root /home/in4matic/example-website-dev/public;
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
How can I fix that.
Moving laravel app from apache to nginx doesn't require many modification, but because laravel uses .htaccess file for url rewrite which won't work in nginx, you have to modify nginx config file so nginx can rewrite url. here is the example of my config file :
server {
listen 80;
server_name your-website.com;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000 # Keep this as per your old config;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have been using this config for all of my laravel app.
and make sure that Storage directory has proper permission.
Recently I set up a virtual machine through vagrant and I'm using nginx as a web server. The OS I'm using for the VM is Ubuntu Linux 12.04. The problem is that any script I write in PHP gets downloaded, instead of being executed on the browser. Seeing as the PHP interpreter is normally installed I figured I'd post here the config file of nginx, as this is where the problem is likely to be found. Being a novice in the world of web development, I can't figure out what is out of the ordinary, can you guys take a look and tell me if you see something wrong? Thanks.
server {
listen 80;
server_name localhost;
root /vagrant/www/web;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
#strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server listening socket
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# enable global phpMyAdmin
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
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 ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
Looks like you're having double // to indicate the fastcgi_pass path of php in your location block, try this instead
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
As was my suspicion the problem was in this location block:
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
The parameter to the location directive is a regular expression that defines only two possible file names: app.php and app_dev.php, thus making any other filename non-executable. For this to change, one needs to either add the names of one's php scripts to the parameter or change the regular expression to a more inclusive form as per Frederic Henri's suggestion.