Setting up multisite with nginx - laravel

Having some problems setting up the following setup with nginx:
example.com
example.com/gb/en
These are laravel sites and the gb/en is the first of more that will be added. Root works obvs but I can't get /gb/en to point at the /gb/en/public folder.
I have tried a bunch of things but this is my current conf file:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
root /home/forge/www.example.com/public;
ssl_protocols TLSv1.2;
ssl_ciphers TEST;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /gb/en {
root /home/forge/www.example.com/gb/en/public;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/www.example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
You can see my location block in there which I have tried a number of different ways but to no avail.
Any assistance would be greatly appreciated.
EDIT:
So I've got one step closer once I realised that the sub folders in the url also get added to the root directory:
location /gb/en/ {
root /home/forge/www.example.com/public;
try_files $uri $uri/ /public/index.php?url=$uri;
}
So, my files are under /public/gb/en/public but the above resolves to /public/gb/en/index.php
How can I get /gb/en/ to resolve to public/gb/en/public/index.php?
Many thanks ;)
EDIT:
The idea came to me suddenly - use a symlink! I symlinked the /gb/en to the public folder in a separate folder and it worked a treat ;)

I would sugest to copy the try_files line into the /en/gb block. In that folder or site you are not specifying the extensions you would like to use as you are doing in the one that works.
Try this:
location /gb/en {
root /home/forge/www.example.com/gb/en/public;
try_files $uri $uri/ /index.php?$query_string;
}
However, if the content of /en/gb is related to the site, setting this up directly in Laravel might be a better option.

I used a symlink in the end to achieve this. I symlinked /gb/en to the public folder in another location and it did what I was after ;)
EDIT - I later found that this also had problems with sub pages but then found this example and it helped to achieve what I wanted to in the first place https://gist.github.com/noeldiaz/08a1211a7c47f21f7083

Related

Nginx 404 Not Found Error. How can I fix it?

I'm using Laravel 8 for my project.
Php: 7.4
Using AWS Nginx Server
My Application's Homepage is fine. The homepage is working well. But, when I'm clicking a link then I'm getting a 404 Not Found error from Nginx.
Here is my server config in nginx.conf
server {
listen 80;
listen [::]:80;
server_name something.com;
root /srv/something.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
What to do? What to change?
How to fix this error?

hosting vue.js and codeignitor with nginx

I have 2 folders dist and api_middleware in '/var/www/public_html/website.com/'
dist folder contains my frontend production code. This folder is symblink-ed from /home/user/frontend/dist
api_middleware folder contains codeignitor code, that we use as middleware for frontend to communicate with our erp. This folder is symblink-ed from /home/user/api_middleware.
I want to host both code with nginx. And this is the code that I came up with.
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/website.com.crt;
ssl_certificate_key /etc/ssl/website.com.key;
server_name website.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
index index.php index.html index.htm index.nginx-debian.html;
location / {
root /var/www/public_html/website.com/dist;
try_files $uri $uri/ /index.html;
}
location /api_middleware {
root /var/www/public_html/website.com;
try_files $uri $uri/ /api_middleware/index.php?/$request_uri;
client_max_body_size 100M;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_read_timeout 300;
}
}
navigating to website.com works. But the api calls with website.com\api_middleware\<PARAMS> is returning a 404
What am I doing wrong?
PS. Using ubuntu 18.4.
My Updated working code:
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/website.com.crt;
ssl_certificate_key /etc/ssl/website.com.key;
server_name website.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
root /var/www/public_html/website.com/dist;
index index.php index.html index.htm index.nginx-debian.html;
location /api_middleware {
alias /var/www/public_html/website.com/api_middleware/;
try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;
client_max_body_size 100M;
}
location ~ /api_middleware/.+\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_read_timeout 300;
}
}
The issue was that using multiple root was messing with nginx to access the directory location my second codebase.
I used alias to change the location to search for my 2nd codebase, while using root to set location of my 1st codebase.
Take a look at the line try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;. I have added 'api_middleware' twice here, even though the location to index.php file is /var/www/public_html/website.com/api_middleware/index.php . This is needed because of a very old bug in nginx.
finally, to process the php files, I changed the URI where to look for the php files to ~ /api_middleware/.+\.php$.
Reference:
NGINX try_files + alias directives
https://serverfault.com/questions/1035733/nginx-difference-between-root-and-alias
https://serverfault.com/questions/684523/nginx-multiple-roots/684605

How to check what is inside nginx root

I am facing a problem where try_files can't recognize static files in my root directory.
I have already checked my root, which is /var/www/public , all the CSS and js files are inside this directory and is not empty. However, nginx fails to recognize and serve my files. It fails to recognize the files, so it passed the request to the .php location block to process uri, which I don't want. I want it to serve the static files directly. Therefore, I would like to check is nginx looking at the same root directory as I think.
Is there any way I can check what is in the directory that nginx check for files? or any way I can figure out what's the problem?
Here is my nginx.conf:
server {
server_name example.com;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
return 301 https://$host$request_uri;
}
server {
server_name example.com;
listen 443 ssl http2;
access_log /var/log/nginx/access.log vhost;
error_log /var/log/nginx/error_log;
include /etc/nginx/mime.types;
root /var/www/public;
ssl_certificate {ssl_certificate location};
ssl_certificate_key {ssl_certificate_key location};
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
root /var/www/public;
try_files $uri $uri/ /index.php?$is_args$args;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass {my_container_name}:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
Please note that I have changed the server name, ssl_certificate info and docker container name for privacy purpose.
I am answering my own question.
The problem was I didn't mount my app volume(the volume that has all code&resources in it) to my nginx container.
Problem solved after I mounted the volume.

How to configure Orchid control panel, and nginx if needed, so that Orchid loads the javascript and css files?

How to configure Orchid control panel, and nginx if needed, so that Orchid loads the javascript and css files?
Under ubuntu 18.04 running vesta control panel, Orchid does not load the javascript and css content at somesite.com/dashboard.
Since nginx properly loads the css and javascript at the somesite.com/ it appears that the nginx conf is not causing it.
Any help would be greatly appreciated.
/home/user/conf/web/somesite.com.nginx.conf:
server {
listen some-server-ip:443 ssl;
server_name somedomain.com www.somedomain.com;
root /home/user/web/somedomain.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php index.html index.htm;
access_log /var/log/nginx/domains/somedomain.com.log combined;
access_log /var/log/nginx/domains/somedomain.com.bytes bytes;
error_log /var/log/nginx/domains/somedomain.com.error.log error;
ssl_certificate /home/user/conf/web/ssl.somedomain.com.pem;
ssl_certificate_key /home/user/conf/web/ssl.somedomain.com.key;
location / {
# added the following line to allow nginx to recognize laravel dynamically created directories
try_files $uri $uri/ /index.php?$query_string;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
error_page 403 /error/404.html;
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
location /error/ {
alias /home/user/web/somedomain.com/document_errors/;
}
location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}
location /vstats/ {
alias /home/user/web/somedomain.com/stats/;
include /home/user/conf/web/somedomain.com.auth*;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;
include /home/user/conf/web/snginx.somedomain.com.conf*;
}
Orchid relies entirely on Laravel settings
server {
listen 80;
server_name example.com;
root /example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Pay attention to the documentation.
Solution: orchid.software/en/docs/installation > Publishing resources: php artisan orchid:link.

nginx config for phpbb and laravel

I need an nginx config that will work for both Laravel and Phpbb.
I have used laravel forge to setup my digital ocean server, and it created this nginx config:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/before/*;
server {
listen 80;
listen [::]:80;
server_name djembefola.org;
root /home/forge/djembefola.org/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:$
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/djembefola.org-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/after/*;
The /public folder is where the front controller index.php of Laravel lives...
Also in the public folder, I have and install of phpbb at
- /public/board
I am upgrading the forum, and as such I need to rum the phpbb installer, which resides at:
localhost/board/install,
which then calls:
localhost/board/install/app.php/update
The above url is then giving a 404 error.
I have read elsewhere that this is because Nginx needs to be configured correctly in order to run the installer.
The sample Nginx config for phpbb is listed here.
So I need to merge these somehow, but so far my attempts have failed.
I tried adding :
location /board/ {
rewrite ^(.*)$ /app.php/$1 last;
}
to the existing laravel nginx file, but that fails. I am aware that I need to put it in the right place in the nginx config, but I fear I'm probably overlooking something else, as I am guessing a bit here...
Can anyone help please?
Based off the example config, you can nest required rules within a block wrap (just like they have done with the /install/ directory). eg:
location /board/ {
try_files $uri $uri/ #rewriteapp;
# Deny access to internal phpbb files.
location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb|store|vendor) {
deny all;
# deny was ignored before 0.8.40 for connections over IPv6.
# Use internal directive to prohibit access on older versions.
internal;
}
# Pass the php scripts to fastcgi server specified in upstream declaration.
location ~ \.php(/|$) {
# Unmodified fastcgi_params from nginx distribution.
include fastcgi_params;
# Necessary for php.
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
try_files $uri $uri/ /board/app.php$is_args$args;
fastcgi_pass php;
}
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
Note that #rewriteapp is outside the block wrap. Nginx will complain if you try to place it inside.
Just amend the paths to match your directory.

Resources