Nginx config for vue app + php symfony backend - php

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;
}
}

Related

nginx: php-fpm is kicking in for main website, but not second site

I have two web folders:
/var/www/mainapplication.com/public
/var/www/helpsystem
they are both PHP sites.
From the main application, you can click on a "help" button that links to https://mainapplication.com/help.php
Problem:
Right now, when someone clicks on the help button, the tries to download the file help.php.
Code:
/var/www/mainapplication.com/help.php looks like this in part:
$url = https://mainapplication.com/help/index.php
$header("Location:$url");
The nginx conf file looks like this:
server {
listen 443 ssl;
root /var/www/mainapplication.com/public;
server_name mainapplication.com;
ssl_certificate /etc/ssl/a/bundle.crt;
ssl_certificate_key /etc/ssl/a/a.key;
ssl_protocols TLSv1.2;
error_log /var/log/nginx/mainapplication_com.log warn;
index login.php;
location / {
allow all;
try_files $uri $uri/ /=404;
}
location ^~ /help {
alias /var/www/helpsystem;
try_files $uri $uri/ /=404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
It seems the php section is not working for the help system, although it is for the main site.
Any tips would be appreciated. right now I'm trying to switch between the alias command and another root command.
EDIT 1
when I change the nginx conf to look like this:
location ^~ /help {
alias /var/www/helpsystem;
try_files $uri $uri/ /=404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I get the following error:
2020/03/24 19:37:06 [error] 9241#9241: *1 rewrite or internal
redirection cycle while internally redirecting to "/=404", client:
198.1.2.1, server: mainapplication.com, request: "GET /help.php HTTP/1.1", host: "mainapplication.com", referrer:
"https://mainapplication.com/widget_settings.php"
Expand the wiki section:
location ^~ /wiki {
alias /var/www/helpsystem;
try_files $uri $uri/ /=404;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location ^~ /help {
alias /var/www/helpsystem;
try_files $uri $uri/ /=404;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Nginx setup for laravel project, which has routes with ".php" endings

I'm trying to setup my Laravel project using Nginx. My /etc/nginx/conf.d/default.conf is:
server {
listen 80;
index index.php index.html;
root /var/www/public;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
I have a problem with routes with ".php" endings, e.g.
Route::get('modules.php', 'ModuleController#index');
Instead of going to index.php and looking there the route, the server tries to open file modules.php, which doesn't exist.
I know, that problem in nginx settings, but I don't have experience with it, so I can't fix it myself.
Look through this page https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04
Here you can find suitable configuration and description.
For example:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Add a try_files statement to the second location block.
For example:
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
try_files $uri /index.php?$args;
fastcgi_pass app:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

nginx 403 forbidden error + mac + laravel

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.

laravel inner pages are not working on AWS EC2 Nginx

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.

Nginx config for 2 PHP applications with url prefixes

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;
}

Categories