nginx config add location lead error - nginx

I met a problem about Nginx really make me confused.
if I set the server as blow :
server {
listen 80;
server_name _;
root /usr/project/exchange/front/build;
index index.html;
location = /{
try_files $uri $uri/ =404;
}
}
if I add location / in the server. the page shows error.
server {
listen 80;
server_name _;
root /usr/project/exchange/front/build;
index index.html;
location = /{
try_files $uri $uri/ =404;
}
location / {
proxy_pass http://127.0.0.1:8088;
}
}
That's really make confused. Nginx match the url from =/ to /?Why add location / leads to mistake?

Related

How to configure multiple react projects using 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;
}
}

Multiple roots nginx

Is it possible to have a suburl that point to a different root? For example:
www.domain.com/ -> /home/ubuntu/project1
www.domain.com/project2 -> /home/ubuntu/project2
I have this configuration at this moment but I'm getting a 404 when resolving domain.com/project2
server {
listen 80;
server_name domain.com;
root /home/ubuntu/project1;
location /project2 {
root /home/ubuntu/project2;
index index.html;
}
location / {
try_files $uri $uri/ /index.html;
}
}
It's because nginx will append the uri to root directive.
In your example config, accessing domain.com/project2 would try to look for a file named project2 in /home/ubuntu/project2 which is not found and return 404.
To solve your problem, try using alias directives.
server {
listen 80;
server_name domain.com;
root /home/ubuntu/project1;
location /project2 {
alias /home/ubuntu/project2;
index index.html;
}
location / {
try_files $uri $uri/ /index.html;
}
}

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

How to set nginx location

here is my question
I am learning nginx recently
And I don't know why I can handle my config about location
here is my nginx.conf
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root /Users/billyliou/web-site;
location / {
root html;
index index.html;
}
location /demo {
index index.html index.htm;
try_files $uri $uri/ /demo/index.html?s=$uri&$args;
}
location /test {
index index.html index.htm;
try_files $uri $uri/ /test/index.html?s=$uri&$args;
}
...
I put two folders into '/Users/billyliou/web-site'
one is demo , the other is test.
Both contain index.html.
And when I type localhost:8080, it shows nginx default page.
When I type localhost:8080/demo it shows demo page.
but when I type localhost:8080/test , it shows demo page too.
the last one is beyond my expectation.
How can I set this config?
Try this
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /demo {
root /Users/billyliou/web-site/demo;
index index.html index.htm;
try_files $uri $uri/ /index.html?s=$uri&$args;
}
location /test {
root /Users/billyliou/web-site/test;
index index.html index.htm;
try_files $uri $uri/ /index.html?s=$uri&$args;
}
location / {
root /Users/billyliou/web-site;
index index.html;
}
...
}

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.

Resources