multiple location in nginx is not working - error 404 - nginx

I am trying to run two nodejs app one on default path (/) and another on /api , I have tried multiple ways but multiple locations are not working with nginx , only root domain (/) works fine, it doesn't matter which app I do assign (2nd or first). and if I try to visit (/api) it return 404 not found and (/) path is working fine.
Here is my default nginx file
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name example.com;
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
location / {
proxy_pass http://localhost:8081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
}
What I am doing wrong. Any help will be apricated. thanks

After Doing a lot of googling and tried many ways I have found the solution and instead of using react app as dynamic loading i generated a build of react and stored that build on a particular location and served these build files on main location with nginx.
location / {
root /home/user/gui/build;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:3002/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /static/ {
root /home/user/gui;
try_files
/gui/build$uri
=404;
}
So here (/) will try files in the root folder which is here (/home/user/gui/build) and we have index.html which is generated by react build this index.html will be served. Now the next problem was to serve react generated static files so i have created a separate location (/static/) with the same logic of / path.

Related

Problem congifuring Nginx to visualize Swagger UI

I have a containerized dotnet service including swagger UI that I can locally run on localhost:7060/swagger/index.html.
I have a problem configuring nginx to point to it. Here is my weaplan.conf file that nginx detects
server {
Listen 80;
location /swagger {
root /var/www/html/weaplanservices/DataHandlerAPI;
proxy_pass http://127.0.0.1:7060;
try_files $uri $uri/ /index.html;
}
}
Note: the project exists in the exact indicated root and the containerized app works correctly
I solved this issue by reconfiguring nginx this way to serve Swagger UI:
server {
Listen 80;
location /swagger {
root /var/www/html/weaplanservices/DataHandlerAPI;
proxy_pass http://127.0.0.1:7060;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Nginx: Nuxtjs app on main domain + WordPress in a subdirectory

I have a nuxtjs app https://damjanun.com/ running on main domain with nginx proxy setup. Now we need to install wordpress inside a sub-directory i.e: https://damjanun.com/blog . I can't get it done.
Found the solution. Here is my nginx config file for damjanun.com. The blog also works fine https://damjanun.com/blog
location /blog {
try_files $uri $uri/ /blog/index.php?q=$1;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

Nginx: How to rewrite url for a particular directory only

I am using nginx for reverse proxy. I am rewriting a directory using an alias. On my alias directory, I have Angular 2 build files which are using html5 routing. So when user will refresh the page it should rewrite to the index.html present in that directory only.
"control-pane is overwrite by an alias"
I already tried the "try_files $uri $uri/ /index.html =404;"
but this was referring to index.html inside main directory.
I also tried to tried to give an absolute path but still, it was not working.
server {
listen 80;
server_name xx.xxx.xx.xxx;
location / {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /control-panel {
alias /var/www/html/soundoff-admin-dev/dist;
# here rewrite should override with index.html inside "dist" directory
}
}
Borrowed from https://stackoverflow.com/a/50863128/1264360
Try,
location /control-panel {
root /var/www/html/soundoff-admin-dev/dist;
try_files $uri $uri/ /control-panel/index.html;
}

Configure Nginx to serve a node js app from a location within root directory

I am trying to serve a Node JS app alongside some static sites that are already functioning and only serving static content (/insta-app is one of the apps, there is another nginx file with another server block for the other ones which sit on different subdomains). I am successfuly getting the server.js app when I navigate to the URL /nodejsapp. The problem is that all the static content the app requires does not get served and comes up as a 404 (images, js files and css). I wrote a location block for nodejsapp/dist where the static content exists but this did not fix the problem. The content is being requested by a handlebars template that gets successfully called and is sitting in a views folder in the root. I am running the whole thing on an ubuntu server, and the node app is running through pm2 which is working fine when I request curl localhost:3000. How can I get the static content into the server?
Nginx server block:
server {
listen 443 ssl default_server;
root /var/www;
index index.html index.htm server.js;
server_name uat.com www.uat.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/uat.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/uat.com/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ /.well-known{
allow all;
}
location /insta-app{
alias /var/www/insta-app/html;
allow all;
}
location /nodeJsApp{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $proxy_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# attempt to serve the static content of the app
location /nodeJsApp/dist{
root /var/www/sydney-sevens/dist;
try_files $uri $uri/ =404;
}
}

Trying to proxy Jira through 2 nginx reverse proxies images and css do not load

I am trying to use nginx to reverse proxy through two proxies I have a server that handles all https traffic and a separate server runs Confluence and Jira. I have the first nginx proxy configured like
ssl on;
ssl_certificate /etc/letsencrypt/live/mydomain.com/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
access_log /var/log/nginx/nginx.mydomain.com.access.log;
error_log /var/log/nginx/nginx.mydomain.com.error.log;
server_name mydomain.com www.mydomain.com;
root /var/www/mydomain.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /jira {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jira.home.mydomain.com;
}
The second nginx proxy is configured as
listen jira.home.mydomain.com:80;
server_name jira.home.mydomain.com;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}
If I go to the second proxy directly JIRA works fine. But trying to go from external to Jira(through the first and second proxy) it doesn't load the css or images.
I am trying to use mydomain.com/JIRA to access JIRA at jira.home.mydomain.com
What am I doing wrong?
As per the official troubleshooting guide, try disabling gzip on nginx since the traffic is already encoded.
Worked for me on jira + ssl + nginx reverse proxy
Seeing other posts the following line may cause the issue:
try_files $uri $uri/ =404;

Resources