How to configure multiple react projects using nginx? - nginx

I have 3 different React projects, pointing to the same IP address with different ports.
Routing works accurately for the first project(default project)
For the other 2, routing works fine if I'm navigating from the very first page of the website.
For an instance, if I'm at some.ip:3000 then I click something and now, I'm at some.ip:3000/page, it works fine
but if I try some.ip:3000/page directly, 404 page is returned.
Following is the nginx configuration - /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name <private-IP>;
root /var/www/<project1>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}
server {
listen 3000;
listen [::]:3000;
server_name <private-IP>;
root /var/www/<project2>;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 8000;
listen [::]:8000;
server_name <private-IP>;
root /var/www/<project3>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}

Related

Can i match www folder structure in nginx?

In my www root folder I have folder app1, app2, app3 and so on. can I match nginx configuration to match the folder with the /appName? for instance 192.168.10.116 should go to the default nginx folder that was set up (nginx Hello world). However 192.168.10.116/app1 should go to app1. In /var/www/app1 I have index.html
server {
listen 80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name 192.168.10.116;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /var/www/app1;
index index.html;
server_name 192.168.10.116/app1;
location / {
try_files $uri $uri/ =404;
}
}

Content linked to domain is not working but with subdomain it is working on nginx

I have created several subdomain so that i can serve the subdomain related specific content. But the problem i am facing is that when i am trying to access the content related directly to my domain am not getting anything in return in nginx. Below is my configuration file
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/myfile/dev/web;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name dev.example.com;
root /usr/share/nginx/myfile/dev/mobile;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa-crm.example.com;
root /usr/share/nginx/myfile/qa-crm;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa.example.com;
root /usr/share/nginx/myfile/qa;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
}
All server block are serving perfectly except the first one which contains the main domain name i.e. (example.com www.example.com)
No idea what i am doing wrong here. I am new to nginx world

nginx config does not take action

I'm trying to setup a basic nginx server. Usually I'm not working with nginx and I ran in to some issues I'm unable to wrap my head around.
I have a debain server with all the necessary things installed (like php, mariadb, ufw,...) and I want to run my website somewhat like this:
xxx.xxx.xxx.xxx -> /var/www/
http(s)://(www).lechner.io -> /var/www/domains/lechnerio/
I want both https and http and the domain with www and without pointing to the folder /var/www/domains/lechnerio and the IP Address pointing to /var/www/
First things first, only getting :80 working.
I have the following config setup:
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
however, when I now try to visit the IP the nginx welcome site is shown. When I access it via domain, it also shows the files from /var/www/ even though i reloaded everything. nginx -t is working. A link from /etc/nginx/sites-available/lechnerio to /etc/nginx/sites-enabled/
Any input very welcome!
try following
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io www.lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.html index.htm;
server_name _; # everything else
location / {
try_files $uri $uri/ =404;
}
}

Nginx configuration causing too many redirects

I have a conf file with example.com as the root. In the example.com directory, there is an html, css, img, and js folder. I understand this deviates from the traditional html directory as root. I have tried many different configurations (using regex based on filetypes, variables, etc.) but I always get too many redirect errors. Can anyone help on a good conf file for this type of directory structure? Here is my conf file currently.
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = error.html?error=404;
location ~ /.well-known {
allow all;
}
location / {
location ~* \.(html|php)$ {
root html/;
}
location ~* \.css$ {
root css/;
}
location ~* \.js$ {
root js/;
}
location ~* \.(png|jpeg|gif)$ {
root img/;
}
try_files $uri =404;
}
}
Thanks in advance for any help!
Here is the configuration I ended up using:
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = /html/error.html?error=404;
location ~ /.well-known {
allow all;
}
location = / {
try_files /html/index.html =404;
}
location / {
location ~* \.(html|php)$ {
try_files $uri /html/$uri =404;
}
location ~* \.css$ {
try_files $uri /css/$uri =404;
}
location ~* \.js$ {
try_files $uri /js/$uri =404;
}
location ~* \.(png|jpeg|gif)$ {
try_files $uri /img/$uri =404;
}
try_files $uri =404;
}
}
My problem was that all of my redirects were using relative pathing (such as try_files html/$uri) instead of absolute pathing from the site root (/html/$uri). This lead to redirects like /html/html/html/...
I thought that if I used an absolute path, it would be absolute to the root of the server, and not the site.
My only issue now is that my parameter on my error page redirect (?error=404) doesn't work with absolute pathing, but that not a huge deal.

nginx how to configure route to html file?

I have the nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/site/public;
index main.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
At root directory I also have html files:page1.html, page2.html, page3.html.
I would like to configure route mysite.com/services/page1 to file page1.html. etc. How can I do it?
I tried it:
location = /services/page1 { try_files /page1.html;}
But it doesn't work.
If you want to rewrite url only if the file doesn't exist you can use named location in try_files directive.
location /services {
try_files $uri $uri/ #service_pages;
}
location #service_pages {
rewrite ^/services/page([1-3]).html /page$1.html;
}

Resources