Nginx multiple projects in one domain blank page laravel - php

This is my config here for Nginx. I have a domain named tstdmn and two Laravel projects first tstdmn.com project and second florist project I want to deploy florist project into the tstdmn.com/florist, I set it all but it returns a blank page at tstdmn.com/florist what's my issue here?! And I know the problem is with my Nginx configuration because I switch the florist project to the main project and it works, it's not from my Laravel configurations
root /var/www/html/tstdmn.com/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name tstdmn.com www.tstdmn.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /florist {
alias /var/www/html/florist/florist_backend/public;
try_files $uri $uri/ #laravel1;
location ~ \.php {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
}
}
location #laravel1 {
rewrite /florist/(.*)$ /florist/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
And so another route of project show's blank like /register

I solved it by someone's solution at laracasts:
using subdomain instead of routing
server { listen 80; listen [::]:80;
root /var/www/html/florist/florist_backend/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name florist.tstdmn.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
..... and some code not related
address of question in laracast https://laracasts.com/discuss/channels/servers/nginx-multiple-projects-in-one-domain-errors

Related

How to route request url subfolder paths to specific php pages using nginx and php-fpm

I'm using a local nginx server for the first time to set up a website i'm building and i'm having trouble setting up the nginx config to handle url requests the way I want. My website serves multiple php pages as the user navigates through the website. When developing the site initially using a local php server, I used GET requests with window.location.href changes for site navigation. For example:
http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3
However, since its going to be an ecommerce website for a small business, I wanted to handle the URLs in a cleaner and more professional manner.
My site structure looks something like this:
Website:
->index.php
->shop.php
->about.php
->product-page.php
->/css/
->/javascript/
->/php/
I want to configure nginx to route url paths in the following way
www.mywebsite.com -> routes to index.php
www.mywebsite.com/shop -> routes to shop.php
www.mywebsite.com/shop/anything -> routes to shop.php
www.mywebsite.com/about -> routes to about.php
www.mywebsite.com/product -> routes to product-page.php
www.mywebsite.com/product/anything -> routes to product-page.php
I've tried numerous suggestions over a couple of days before asking here but everything failed due to one reason or another, 404s, 500 internal errors, and redirect loops. I'm hoping to gain some inside here while I move onto other aspects of the site, so as to stop beating my head against the wall. Here is the state of my nginx conf at this moment:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location = /shop {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index shop.php;
try_files $uri /shop.php;
}
location /shop/ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
try_files $uri /shop.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
How could I go about solving this? And if there is a better standard in structuring a website and its URLS please let me know. This is my first website and first time using nginx - so i'm a little naive on best practices.
If you need a certain php script to be responsible for a whole path, you need a config like this:
root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
index /shop.php; # be careful with path to the file here
}
Although I would rather recommend a more traditional and cleaner project structure.
In your project root create two directories: shop and product. Move shop.php and product-page.php into designated folder and rename both to index.php. Your nginx config for this structure will be like this:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
index index.php;
try_files $uri $uri/ =404;
}
location /shop/ {
try_files $uri $uri/ /shop/index.php?$args;
}
location /product/ {
try_files $uri $uri/ /product/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Configuring php with nginx inside a reactjs app

I have a nginx configuration for a react app. I however would also like to include a sitemap.php that I build dynamically with php.
So here is my nginx config:
server {
listen 80;
listen [::]:80;
server_name mysite.com;
index index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location /sitemap.xml {
alias /var/www/web-app/public/sitemap.php;
}
location / {
root /var/www/web-app/public;
try_files $uri $uri/ /index.html;
default_type "text/html";
}
}
The snippets file consist of this:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
Also, this is hosted on an Ubuntu 16.04 digitalocean VPS.
My react app still loads fine. It is based on the index.html in my site root (/var/www/web-app/public). If I put test.php in the public folder, I get a 404 error. For my sitemap.xml alias, it forwards correctly to sitemap.php (also in public) but the php does not render.
So my two biggest issues here:
1. Why am I getting a 404 on /mysite.com/test.php?
2. And why is my php not rendering when it does work? (i.e. sitemap.php)
You are missing a root statement for your location ~ \.php$ block, so your PHP files will not be found. As this seems to be a common root with the location / block, simply move the statement up to server block scope:
root /var/www/web-app/public;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location / {
try_files $uri $uri/ /index.html;
default_type "text/html";
}
There are a number of ways to redirect /sitemap.xml to /sitemap.php, but a rewrite...last will be simplest and invisible to users:
location = /sitemap.xml {
rewrite ^ /sitemap.php last;
}
See this document for location syntax, and this one for the rewrite directive.

nginx + wordpress in subfolder + debian configuration

In a subfolder on a domain I want to install a wordpress blog. I use nginx. The URL to access the blog should be like this: example.com/blog
site config looks as follows:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location /blog {
alias /var/www/example.comblog/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ /blog/.+\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
The wordpress files reside in the folder
/var/www/example.comblog/html. When accessing example.com/blog,
the browser shows a 404 error.
In /etc/php5/fpm/php.ini I adapted this: cgi.fix_pathinfo=0
nginx version: nginx/1.6.2
/var/log/nginx/error.log does not show anything of interest
UPDATE 1:
After setting error logging to debug, (among others) the following lines appear. Maybe this helps:
open index "/var/www/example.comblog/html/index.php"
internal redirect: "/blog/index.php?"
rewrite phase: 1
test location: "/blog"
test location: ~ "/blog/.+\.php$"
using configuration "/blog/.+\.php$"
http script var: "/blog/index.php"
trying to use file: "/blog/index.php" "/var/www/example.com/html/blog/index.php"
The internal redirect seems incorrect? And in the last line there should be /var/www/example.comblog/html/blog/index.php instead of /var/www/example.com/html/blog/index.php. I suspect this is the reason for the 404. Because the index.php does not exist at /var/www/example.com/html/blog/index.php.
Update 2:
Okay there seems to be a long standing issue with using alias together with try_files.

Directory Override with NGINX

I need a special directory override of NGINX rules on my web server so that the rules that apply to /var/www/acme regarding CSS, JS, etc. do not apply to /var/www/acme/special. I have a /var/www containing my website under me.com (127.0.0.1 localhost setup). Inside there, I have an acme folder that uses a special framework for my PHP. However, I have a folder /var/www/acme/special that needs to not apply those same rules and just run like a regular PHP website. I'm stuck because when I apply the logic with a special location and try_files routine, along with a break, for /var/www/acme/special, then I get an HTTP 500 error that says "rewrite or internal redirection cycle when internally redirecting to '/acme/special////////////".
Here's my /etc/nginx/sites-available/me.com file for NGINX on Ubuntu 16.04. What am I doing wrong?
server {
listen 80;
listen [::]:80;
root /var/www;
index index.php index.html;
server_name me.com www.me.com;
location /acme/special {
try_files $uri $uri/;
break;
}
location ~ ^/acme/(.*(?<!boot))/css/(.*)$ {
alias /var/www/acme/$1/views/assets/css/$2;
}
location ~ ^/acme/(.*(?<!boot))/js/(.*)$ {
alias /var/www/acme/$1/views/assets/js/$2;
}
location ~ ^/acme/(.*(?<!boot))/img/(.*)$ {
alias /var/www/acme/$1/views/assets/img/$2;
}
location ~ ^/acme/(.*)/fonts/(.*(?<!boot))$ {
alias /var/www/acme/$1/views/assets/fonts/$2;
}
location ~ ^/acme/(.*)/boot/(.*)$ {
alias /var/www/acme/$1/views/assets/boot/$2;
}
location / {
try_files $uri $uri/ #php;
}
location #php {
rewrite ^/acme/([^\/]+)/.*((?!index\.php).)*$ /acme/$1/index.php?query_string last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Try:
location /acme/special {
try_files $uri $uri/ =404;
}
The last parameter of the try_files statement is the default action, so the $uri/ term is not being treated as one of the file terms. Use =404 or something like /index.php depending on your desired file-not-found response. See this document for more.
The break is part of the rewrite module and has no purpose here.

NGINX configuration for static and PHP files

I am trying to configure nginx to serve static and PHP files. The config I have isn't working. I want the following local folder structure:
src/static/ -> contains HTML, CSS, JS, images etc
src/api/ -> contains PHP files for a small REST service
If I visit http://mysite.local I want to be served files from the /static folder. If I visit http://mysite.local/api I want to be served the API PHP files. I want the requests to the api to be re-written and sent to an index.php file.
Some examples:
http://mysite.local/test.html -> served from src/static/test.html
http://mysite.local/images/something.png -> served from src/static/images/something.png
http://mysite.local/css/style.css -> served from src/static/css/style.css
http://mysite.local/api/users -> served from src/api/index.php?users
http://mysite.local/api/users/bob -> served from src/api/index.php?users/bob
http://mysite.local/api/biscuits/chocolate/10 -> served from src/api/index.php?biscuits/chocolate/10
The below config works for static files but not for the api files. I get a 404 error back if I visit one of the API paths.
server {
listen 80;
server_name mysite.local;
access_log /var/log/nginx/mysite.access.log main;
error_log /var/log/nginx/mysite.error.log debug;
location / {
index index.html;
root /var/www/mysite/src/static;
try_files $uri $uri/ =404;
}
location /api {
index index.php;
root /var/www/mysite/src/api;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
The initial problem is the root directive in the location /api block, which should not include the location component as this gets appended as part of the URI, so:
location /api {
root /var/www/mysite/src;
...
}
will result in a local path of /var/www/mysite/src/api/index.php when presented with the URI /api/index.php. See this document for details.
The try_files rule does not rewrite the URI as you specify in your example. If you really need the final path of the URI to be presented as a query string to /api/index.php you will need to use rewrite.
The simplest solution (if you do not need to serve static content from that location) is to replace your try_files with:
location /api {
...
rewrite ^/api/(.*)$ /api/index.php?$1 last;
location ~ \.php$ { ... }
}
Otherwise, use a named location:
location /api {
...
try_files $uri $uri/ #rewrite;
location ~ \.php$ { ... }
}
location #rewrite {
rewrite ^/api/(.*)$ /api/index.php?$1 last;
}
See this and this for details.

Categories