nginx custom root for certain addresses - laravel

I have installed nginx to my VPN. And I'm a very beginner with that so it might be a stupid mistake but I was unable to figure our or google it.
What I'm trying to do and keep failing in it is set the root folder for certain addresses. For example:
mydomain.com/websiteone/ will have root folder in /var/www/websiteone/public/ and
mydomain.com/websitetwo/ in /var/www/websitetwo/public/
Im using Laravel that why I need to do that.
Here is the confing and what I was attempting in different variations.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
index index.html index.php index.htm;
location /websiteone/ {
root /var/www/websiteone/public/;
try_files $uri $uri/ 404;
}
location /websitetwo/ {
root /var/www/websitetwo/public/;
try_files $uri $uri/ 404;
}
}

No need for alias or anything fancy as long as URL dir path matches to actual directory names of your two Laravel instances (see below).
Also, Laravel has a typical "front controller" URL pattern, which means URLs that do not exist should be bootstrapped through its index.php. So:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
index index.html index.php index.htm;
location /websiteone/ {
root /var/www/websiteone/;
try_files /public$uri /public$uri/ /public/index.php$is_args$args;
}
location /websitetwo/ {
root /var/www/websitetwo/;
try_files /public$uri /public$uri/ /public/index.php$is_args$args;
}
}

Related

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

Why subdomain is not working in Ubuntu nginx server?

There a running Laravel application in the root folder of my ubuntu nginx server. Now i am trying to create a subdomain and run another Laravel application there. For instance,
findosman.xyz (root, running laravel app)
api.devport.findosman.xyz (another Laravel API)
Here is my current configuration;
In /etc/nginx/sites-available/api.devport.findosman.xyz
server {
listen 80;
listen [::]:80;
root /var/www/api-devport;
index index.php index.html index.htm;
server_name api.devport.findosman.xyz;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
}
Here is the /etc/nginx/sites-enabled/api.devport.findosman.xyz
server {
listen 80;
listen [::]:80;
root /var/www/api-devport;
index index.php index.html index.htm;
server_name api.devport.findosman.xyz;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
}
In /var/www/api-devport I have currently only a index.php file which only has <?php echo "hello world"?>
the root folder project is running fine. but wherever I am trying to access devport.findosman.xyz it shows The site can't be reached.
How to overcome this problem ? Thanks in advance.

Deploying Vue Cli 3 SPA with Laravel backend

I have a Vue Cli 3 SPA application which makes api calls to a Laravel Backend. I've created a LEMP droplet on DigitalOcean, and I've cloned the two projects in the /var/www/html directory. api/ for the backend, web/ for the frontend. I've configured nginx root to web/dist/index.html. Now, how can I make api calls, since the root of the project is index.html?
I've searched a lot. I saw solutions where I must copy the dist folder's contents to api/public, and adjust nginx's root to be api/public/index.html. But that doesn't change the fact that I still can't make api calls, because index.php is never reached.
Could you please advice me how you do it? Should I create a subdomain?
Thanks!
UPDATE
I've tried this according to oshell's answer:
# For the vue app
server {
listen 80;
root /var/www/html/web/dist;
index index.html;
server_name XXX.XXX.XX.XXX # the ip addreess that I have
error_page 404 /;
location / {
try_files $uri $uri/ /index.html;
}
}
# for the laravel application
server {
listen 80;
root /var/www/html/api/public;
index index.php;
server_name XXX.XXX.XX.XXX/api;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Now whatever I open, it just goes to the vue application. If I try to make an api call to XXX.XXX.XX.XXX/api/something from the vue app, I've got 405 Method not allowed
You need to setup two separated servers for frontend and backend. You could make api reachable via api.example.com and frontend via example.com. The nginx config should look something like this:
#laravel.conf
server {
listen 80;
root /var/www/html/project_name/api;
index index.php index.html index.htm;
server_name api.example.com www.api.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
#vue.conf
server {
listen 80;
root /var/www/html/project_name/web/dist;
index index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.html;
}
}
You could also direct all traffic to you index.php and set it up so Route::any('/') returns the static page, including the static assets and all api routes are handled via Route::any('/api/foo').
The following configuration works for me on local environment - home directory on ubuntu.
Folder structure
example/dist - vue application
example/laravel - laravel api application
example/laravel/public - laravel public directory
example/laravel/public/images - laravel api images directory
Urls
example.lo - vue application
example.lo/api - laravel api
server {
# server name and logs
server_name example.lo;
access_log /var/log/nginx/example.lo_access.log;
error_log /var/log/nginx/example.lo_error.log;
root /home/username/example/laravel/public/;
index index.html index.php;
# location for vue app
location / {
root /home/username/example/dist/;
try_files $uri $uri/ /index.html;
}
# location for laravel api
location /api {
try_files $uri $uri/ /index.php$is_args$args;
}
# location for api images
location /images {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}

Nginx configuration for angular and magento

I want to do nginx setup for handing two project with same domain.
Example domain: example.com
Angular project should run with example.com
magento2 project should run with example.com/shop
I tried the code below, but its not working.
location /shop {
alias /var/www/www.example.com/shop/;
index index.html;
try_files $uri $uri/
autoindex on;
}
Can please someone help me to do this.
You should use official NGINX configuration sample as provided here.
Naturally, you will prefix all the Magento 2 locations with /shop/, for your specific case.
So you will end up with this kind of config:
server {
listen 80;
server_name example.com;
location / {
root /path/to/your/generated/angular/resources;
try_files $uri$args $uri$args/ /index.html;
}
# Magento 2 directives start here...
location ~* ^/shop/setup($|/) {
# ...
}
# The rest of Magento 2 directives...
}
You can start with the following config to serve your Applications:
server {
listen 80;
server_name example.com;
location / {
root /path/to/your/generated/angular/resources;
try_files $uri$args $uri$args/ /index.html;
}
location /shop {
root /path/to/shop/;
index index.html;
try_files $uri $uri/;
}
}
I'm not 100% sure if the shop route will work. Maybe you need to configure php to serve it. Therefore you can follow this official example.
If you want to serve also to www.example.com you can set server_name *.example.com (docs).

Nginx serving non-secure resources on https domain

I'm having some issues setting up a server with an SSL certificate. I was able to install the certificate just fine and restarted the nginx service. However, when I attempt to load my website, I see that all img, css and js files are being retrieved with http instead of https. This is a Magento website. Is there something wrong with my conf file?
server {
listen 80;
server_name www.my-domain.com;
return 301 $scheme://my-domain.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/ssl/my-domain/my-domain_com.pem;
ssl_certificate_key /etc/ssl/my-domain/my-domain_com.key;
access_log /var/log/nginx/magento.local-access.log;
error_log /var/log/nginx/magento.local-error.log;
server_name my-domain.com;
root /var/www/my-domain;
include conf/magento_rewrites.conf;
include conf/magento_security.conf;
# PHP handler
location ~ \.php {
## Catch 404s that try_files miss
if (!-e $request_filename) { rewrite / /index.php last; }
## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
# By default, only handle fcgi without caching
include conf/magento_fcgi.conf;
}
# 404s are handled by front controller
location #magefc {
rewrite / /index.php;
}
# Last path match hands to magento or sets global cache-control
location / {
## Maintenance page overrides front controller
index index.html index.php;
try_files $uri $uri/ #magefc;
expires 24h;
}
rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
location /lib/minify/ {
allow all;
}
}
maybe cause this is how they are called, if you need to serve every thing as https, i would create an empty server that listens on 80 and redirect to https
server {
# listen 80; delete this part
listen 443 ssl;
# the rest of the config
}
# add this server
server {
listen 80;
server_name example.com;
location / # or a more specific location '~ \.(jpg|css|js|jpeg|png|gif)' {
return https://example.com$request_uri;
}
}
or just fix the css location, it might be an absolute URL with http

Resources