how to configure my nginx config file for three apps on the same server - node.js

hello i have three apps (react,angular & node ) i want to configure my nginx file to have this :
mysite.com for react, mysite.com/admin for angular,pmysite.com/api for node app
#node app
location /api/ {
proxy_pass http://127.0.0.1:3000;
access_log /var/log/nginx/mysite.log;
error_log /var/log/nginx/mysite_error.log;
}
#angular app
location /admin {
root /var/www/mysite/backoffice/dist/index.html;
access_log /var/log/nginx/mysite_bo.log;
error_log /var/log/nginx/mysite_bo.log;
}
location ~ \.(aac|m3u8|ts) {
root /var/www/mysite/media;
}
location /uploads/ {
root /var/www/mysite/;
}
#react app
location / {
proxy_pass http://127.0.0.1:3006;
access_log /var/log/nginx/mysite_react.log;
error_log /var/log/nginx/mysite_react.log;
}}
/admin not working it redirect to / any solution ?

The root path under the location /admin block serves as the base path for all the subsequent requests matching that pattern. Looks like you've hardcoded the /admin path to serve a single file /var/www/mysite/backoffice/dist/index.html.
Try changing it to see if this works:
location /admin {
root /var/www/mysite/backoffice/dist; # change to this
access_log /var/log/nginx/mysite_bo.log;
error_log /var/log/nginx/mysite_bo.log;
}

Related

Nginx access to images in backend folder

My app store images on the backedn side in nodejs folder: /images.
After publish it to Ec2 I don't have an access to these images by url like: www.domain.com/image01.jpg
Configuration in /etc/nginx/sites-available/default:
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root /opt/front-end;
try_files $uri /index.html;
}
location /images/ {
root /opt/back-end/;
}
location /api/ {
proxy_pass http://localhost:4000/;
}
}
Is it the configuration problem or permission or something else? I will be grateful for help!
The correct configuration should be:
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root /opt/front-end/;
try_files $uri /index.html;
}
location /api {
proxy_pass http://localhost:4000;
}
location /images {
autoindex on;
alias /opt/back-end/images/;
}
}

Nginx location /yyy redirected to root location

I have 2 locations in my nginx conf like so :
upstream server {
server 127.0.0.1:XXXX;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate XXXX;
ssl_certificate_key XXXX;
location /yyy {
proxy_pass http://server/YYY/;
...
}
location / {
proxy_pass http://server/XXX/;
...
}
}
But it seems that https://example.com/yyy redirects to http://server/XXX/ instead of http://server/YYY/ as if nginx considered it as a subpath of mydomain.com and not a different location.
However if I try to access https://example.com/yyy/ then it redirects to http://server/YYY/.
I would like that both https://example.com/yyy/ and https://example.com/yyy redirect to http://server/YYY/

Nginx changing path

I have been searching thoruhg nginx documentation and posts online but I can't find the answer to this.
I am running a python application and I want one of the paths altered slightly. I can't do this in python for various reasons.
I want the input url:
/public/web/apidocs***
(where * could be anything - including nothing)
to be passed to the python app as
/apidocs*
This is my configuration:
server {
listen 80;
server_name localhost; ##ignored if there is only one server block
charset utf-8;
client_max_body_size 75M;
location = /frontend/webfrontendConnectionData {
try_files $uri #yourapplication;
}
location /public/web/frontend {
alias /frontend/;
autoindex off;
}
location /public/web/adminfrontend {
alias /adminfrontend/;
autoindex off;
}
location ^(/public/web)(/apidocs.*)$ {
try_files $2 #yourapplication;
}
location / {
try_files $uri #yourapplication;
}
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/app/uwsgi.sock;
}
}
Update based on answer so far:
Thank you for pointing out the mistake or missing out the ~ in the config.
The config has changed as follows:
location ~ ^(/public/web)(/apidocs.*)$ {
try_files $2 #yourapplication;
}
location / {
try_files $uri #yourapplication;
}
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/app/uwsgi.sock;
}
Unfortunately it still doesn't work as expected:
wget 127.0.0.1:80/apidocs -> Goes to python App and works as expected
wget 127.0.0.1:80/apidocs/ -> Goes to python App and works as expected
wget 127.0.0.1:80/apidocs/swagger.json -> Goes to python App and works as expected
wget 127.0.0.1:80/public/web/apidocs -> FAILS I want this to give the same response as wget 127.0.0.1:80/apidocs
wget 127.0.0.1:80/public/web/apidocs/ -> FAILS I want this to give the same response as wget 127.0.0.1:80/apidocs/
wget 127.0.0.1:80/public/web/apidocs/swagger.json -> FAILS I want this to give the same response as wget 127.0.0.1:80/apidocs/swagger.json
All the failed responses give me:
robert#ansiblerunner:~/t$ wget 127.0.0.1:80/public/web/apidocs/swagger.json
--2019-04-08 09:50:39-- http://127.0.0.1/public/web/apidocs/swagger.json
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 404 NOT FOUND
2019-04-08 09:50:39 ERROR 404: NOT FOUND.
Can anyone suggest the correct syntax for the rule I want.
Update #2
After reading more nginx documentation I found out that it doesn't take the location rules in order and use the first one it hits, rather it has a complex longest match algorithm. I was worried the "location /" block was then overriding all the paths so I have altered the config so it is like the following:
location /api/public {
try_files $uri #yourapplication;
}
location /api/authed {
try_files $uri #yourapplication;
}
location ~* ^(/public/web)(/apidocs.*)$ {
try_files $2 #yourapplication;
}
#location / {
# try_files $uri #yourapplication;
#}
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/app/uwsgi.sock;
}
/api/public and /api/authed both work as normal.
it's the /public/web/apidocs that doesn't. The difference is that I need nginx to change the path provided to the python app in this case. This is why I have a regexp with two parts and it passing $2 rather than $uri. I don't think it's doing what I expect which is to pass /apidoc to the app.
I don't see a regex, there, as it lacks a tilde.
You probably want a clause that starts with:
location ~ ^(/public/web)(/apidocs.*)$ {
You didn't mention which docs you were reading.
Consulting a tutorial,
Understanding Nginx Server and Location Block Selection Algorithms,
we see the syntax is:
location optional_modifier location_match {
and we optionally can specify:
~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match.
I cannot explain your reported 404 symptom,
as the location / prefix clause should have absorbed the GET request
and at least given #yourapplication an opportunity to log the raw URL.
I have an answer
I was completely off base with my understanding of what try_files did not work as I expected. I found an article saying if I use it with an underscore it won't try files and will just use it's fallback.
I also found out that I needed to use rewrite to change the path.
Getting rid of the root location may have not been needed but I prefer not to have it in any case.
My final config is as follows:
server {
listen 80;
server_name localhost; ##ignored if there is only one server block
charset utf-8;
client_max_body_size 75M;
#location = /frontend/webfrontendConnectionData {
# try_files $uri #yourapplication;
#}
location /public/web/frontend {
alias /frontend/;
autoindex off;
}
location /public/web/adminfrontend {
alias /adminfrontend/;
autoindex off;
}
# try_files _ #xxx means don't look at any file go to xxx block
location /api/public {
try_files _ #yourapplication;
}
location /api/authed {
try_files _ #yourapplication;
}
location /public/web/apidocs {
rewrite ^/public/web(/apidocs.*)$ /$1? break;
try_files _ #yourapplication;
}
#location / {
# try_files _ #yourapplication;
#}
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/app/uwsgi.sock;
}
}
Thanks to J_H for comments and answers.

Serve root static file with nginx as node reverse proxy

I have a nodejs server that's served with nginx as reverse proxy. That part is ok, and static files locations are set up correctly. But I want the root address to serve a static html file, and I don't know how to configure nginx so that the root url is not redirectected to the node app. Here's my server block:
upstream promotionEngine {
server 127.0.0.1:3001;
}
server {
listen 3000;
server_name localhost;
root C:/swaven/dev/b2b.pe/promotionEngine/templates/;
index index.html;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://promotionEngine;
proxy_redirect off;
}
location /public/ {
alias C:/swaven/dev/b2b.pe/promotionEngine/public/;
}
location /assets/ {
alias C:/swaven/dev/b2b.pe/promotionEngine/assets/;
}
}
htttp://localhost:3000/ping and http://localhost:3000/public/js/riot.js are correctly served.
But http://localhost:3000 keeps being sent to the node server, where I would like it to return a static index.html. If I remove the / location bloc, the html file is correctly served. How would I configure the location to work as reverse proxy for all urls except the root one ?
UPDATED: (based on comments and discussion)
You'll need 2 exact location blocks. One to intercept the / location and another to serve just /index.html.
An exact location block is described on nginx docs:
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.
Simply using the index directive does not work. Because nginx creates an internal redirect to allow other blocks to match index.html. Which gets picked up by your proxy block.
upstream promotionEngine {
server 127.0.0.1:3001;
}
server {
listen 3000;
server_name localhost;
# Do an exact match on / and rewrite to /index.html
location = / {
rewrite ^$ index.html;
}
# Do an exact match on index.html to serve just that file
location = /index.html {
root C:/swaven/dev/b2b.pe/promotionEngine/templates/;
}
# Everything else will be served here
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://promotionEngine;
proxy_redirect off;
}
location /public/ {
alias C:/swaven/dev/b2b.pe/promotionEngine/public/;
}
location /assets/ {
alias C:/swaven/dev/b2b.pe/promotionEngine/assets/;
}
}
You can use =/ this type of location have higher priority due to lookup:
location =/ {
root ...
}
This request will not even try to reach other locations.
Something like this, adjust for your own use case.
http {
map $request_uri $requri {
default 1;
/ 0;
}
...........
server {
listen 80;
server_name www.mydomain.eu;
root '/webroot/www.mydomain.eu’;
if ($requri) { return 301 https://www.mydomain.eu$request_uri; }
location / {
..........
}
}

How to use nginx proxy_pass subroutes from node app?

I have a node app running on port 8002 with different subroutes like '/login' or '/facebook', i also have nginx (v1.6.0) and the following config:
server {
listen 80;
server_name my-ghost-blog.com ;
client_max_body_size 10M;
location / {
proxy_pass http://localhost:2368/;
proxy_set_header Host $host;
proxy_buffering off;
}
location ~ ^/(sitemap.xml) {
root /var/www/ghost;
}
location ~ ^/(robots.txt) {
root /var/www/ghost;
}
#proxy to a node app running on 8002 port
location ^~ /auth/ {
proxy_pass http://localhost:8002/;
}
}
when i go to '/auth/' it works, but when i try to go to a node app'subroute, 404 appears because nginx dont know how to handle it.
any ideas?
thanks

Resources