Nginx configuration for angular and magento - 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).

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.

nginx custom root for certain addresses

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

laravel project with nginx errors

I have upload my laravel project to remote linux host .This project works well on my local windows homestead.But when I put it on linux host,it does not work .
The project root is :/var/www/html/blog . the nginx config as follwing:
`server
{
listen 80;
#listen [::]:80;
server_name www.antichina.us ;
index index.html index.htm index.php default.html default.htm default.php;
root /var/www/html/blog/public;
# include other.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
`
when I visit the project with the domain,(http://mydomain),it give an http 500 error. but when I set the root as /var/www/html/blog in the nginx config file, the index page of the project is working by visit: http://mydomain/public, other routes are all return 404 error.
I have tried my method,but it seems none is working.Any on ecould help me?thank you !
Add this to your nginx config
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Your index line only needs to this also:
index index.php;
What's in this this file?
include enable-php.conf;

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