Nginx multiple Apps on same port reverse proxy - nginx

I'm trying to run a django app and and angular one on my VPS using Nginx. Below is my config file code:
server {
listen 80;
server_name www.the-patron.com the-patron.com;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location /staticfiles/ {
root /root/thepatron/The-Patron-Backend;
}
# Django Backend
location /back/ {
include proxy_params;
proxy_pass http://unix:/root/thepatron/The-Patron-Backend/thepatron.sock;
}
# Angular Frontend
location / {
proxy_pass http://localhost:4200/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
}
Here my Angular app is running well and the Django one isn't. If I change location /backend/ to location / on line 12 and location / to location /frontend/ on line 18 then I will get the Django app to run while the Angular app will not.
How can I run both and change the location of each?

As my previous approach to host my Angular and Django apps separately was totally wrong. I finally managed to solve my issue thanks to the comments of #Vipulw above.
What worked for me is that I built my Angular application to production and placed the generated build folder inside my static files folder in my Django app directory, and then configured my Nginx to serve that Angular build.
Below is my new Nginx config file:
# Angular Reverse Proxy
server {
listen 80;
server_name <Domain name or IP address>;
root /root/<path to Django app>/static/<Angular app build folder>;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
# Django Reverse Proxy
server {
listen 8080;
server_name <Domain name or IP address>;
location / {
include proxy_params;
proxy_pass http://unix:/root/<path to Django app>/<myapp>.sock;
}
}
Additional Note:
In this way mentioned above, my Angular app is running on port 80 while my Django app is running on port 8080. Everything is working fine this way but I don't see that as the best way to do run both Angular and Django. After a little bit of research, I found that the best way to serve Angular and Django in one app is to let the default Django route to point to the Angular build. The default Django route right now is pointing to the Django Rest Framework root directory.
Sadly, I don't know how to let Django's default route to point to the Angular build file at the moment.

Related

Nginx configuration for 2 apps on same domain, one static, one SSR

I have 2 apps.
App1 is a ReactJS single page app and I want it as a homepage at www.mydomain.com
App2 has a nodejs back-end exposing graphql APIs + a ReactJs front-end and I want this app's homepage to be rooted at www.mydomain.com/app2
Since app2 has multiple pages, I want them to be available at www.mydomain.com/app2/page1, www.mydomain.com/app2/page2, etc.
Here is how I deployed these 2 apps:
sudo pm2 serve /var/www/mydomain.com/app1 --spa
sudo pm2 start /var/www/mydomain.com/app2/backend/index.js
cd /var/www/mydomain.com/app2/frontend ; sudo pm2 start npm -- run "prod"
App1 runs at port 8082
App2 back-end runs at port 4000
App2 front-end runs at port 3003
My Nginx config file looks like this:
server {
root /var/www/mydomain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ =404;
}
location /app2 {
proxy_pass http://localhost:3003;
}
location /graphql {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:4000/graphql;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
App1 is displaying as expected at www.mydomain.com.
Only the homepage of App2 is displaying at www.mydomain.com/app2, but navigating to any other page leads to a 404 error.
I understand with my current configuration, Nginx is looking for App2's pages in the root directory of App1 and doesn't find them. Since App2 is running with npm and doesn't have a an index.html file in its build folder, I cannot get Nginx to look for an index.html file for App2.
What is the right way to get my two applications to work on this domain?

nginx not finding css file

I have just put my flask app on ubuntu. I am using gunicorn and nginx. The app is loading and nginx can find the templates. However the CSS files are not being served. If I enter the URL to access the CSS file in the browser I get a 403.
Here is my config file
server {
listen 80;
server_name myip;
location /static {
alias /home/ubuntu/enrich/website/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
Any idea where I am going wrong?
Thx
S

Static files cant see flask server

I am new to Nginx and I currently trying to deploy my web app on my server.
I have static files (built from react) being served by Nginx. The static files make calls to port 5000, my flask server. When testing, flask cannot receive any calls from my static files.
The (static-flask) setup runs on my local machine, so I am assuming that there is a problem with my config with Nginx.
Here is my Nginx setup (in sites-enabled):
server {
listen 80;
server_name MY_IP_ADDRESS;
location / {
root MY_LOCATION_TO_STATIC_FILES;
index index.html;
try_files $uri /index.html;
}
}
I am guessing that once Nginx serves the static files, the client (static files) make calls to localhost:5000, but does not refer to port 5000 on my server?
How would I serve the static files such that they can refer to the server's localhost:5000?
Edit
I guess I should be more specific with my project. I want to serve my static files when the user hits www.mydomain.com, and when the user interacts with the website, they make calls to a flask server running on port 5000 on my server.
I could consider serving static files from flask, but that would be highly inefficient.
Use nginx proxy:
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:5000;
}
Try this
server {
server_name www.yourdomain.com;
location /static {
alias /home/user/path/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
Change alias to your path project

NGINX Not Loading Resources for Second Server

I'm new to NGINX (well hosting in general), so please excuse my ignorance. I have a Phoenix web app running on localhost:4000 and an ASP.NET web app running on localhost:5123. I'm trying to utilize NGINX to create a reverse proxy so that either web app can be accessed from the same domain. My NGINX config file contains the following:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:4000;
}
location /test {
rewrite /test(.*) /$1 break;
proxy_pass http://localhost:5123;
}
}
I'm able to access server 1 on example.com/ and server 2 on example.com/test, however, server 2 is not able to load its own css, javascript and image files. Is there a way to ensure server 1 and server 2 utilize their own resources via the NGINX config setup?
You are on the right way. There are some lines missing on your second service:
Replace...
location /test {
rewrite /test(.*) /$1 break;
proxy_pass http://localhost:5123;
}
with...
location /test {
rewrite /test/(.*) /$1 break;
rewrite ^/test$ /test/ permanent;
proxy_pass http://localhost:5123/;
proxy_redirect / /test/;
proxy_set_header Host $host;
proxy_buffering off;
}
I don't know if this is the best way to achieve this. Long time ago I used this configuration to implement an etherpad-service on my server. That time I had not the option to use a subdomain. Anyway - using a subdomain for your second service would be better.

Change document root on RStudio_AMI

It is on an amazon server so I checked the following post:
Changing Apache document root on AWS EC2 does not work
and
How to edit httpd.conf file in AMAZON EC2
or in general: How do I change the root directory of an apache server?
Well the information provided did help me so far.
The only file I could find in the etc/apache2 folder is the following:
Edit: The content of the config file is:
"Alias /javascript /usr/share/javascript/
Options FollowSymLinks MultiViews
"
I asked two month ago on his site: http://www.louisaslett.com/RStudio_AMI/, but didnt get an answer.
My question: How can i change the document root on an RStudio AMI server, so that I can change the directory of the rstudio login page away from the root directory to - say - domain.com/login and have a landing page + other folders on the root (domain.com).
Thank you for your help!
Edit:
After the answer from Frédéric Henri and edit:
Here is the content of my rstudio.conf file.
location / {
proxy_pass http://localhost:8787;
proxy_redirect http://localhost:8787/ $scheme://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
access_log /var/log/nginx/rstudio-access.log;
error_log /var/log/nginx/rstudio-error.log;
}
Assuming i have the index.html file in the directory /home/idx/index.html, how would i change the file then.
The following didnt work for me:
proxy_pass http://localhost/home/idx;
proxy_redirect http://localhost/home/idx/ $scheme://$host/;
Or:
proxy_pass /home/idx;
proxy_redirect /home/idx/ $scheme://$host/;
and where would i configure to redirect my rstudio login to.
Thank you!
You are right and looking at the right place if you were using apache2/httpd web server; but in the case of the RStudio AMI it uses nginx web server so all configuration are stored in /etc/nginx
You can review Configure nginx with multiple locations with different root folders on subdomain to see how you can work with the conf file
In your current configuration, it is defined mainly 3 locations:
http://<web_server_ip>/
The conf file used for this case is /etc/nginx/RStudioAMI/rstudio.conf It processes all request and forward to http://localhost:8787 where rstudio is running.
http://<web_server_ip>/julia
The conf file used for this case is /etc/nginx/RStudioAMI/julia.conf It processes all request and forward to http://localhost:8000 where julia is running.
http://<web_server_ip>/shiny
The conf file used for this case is /etc/nginx/RStudioAMI/shiny.conf It processes all request and forward to http://localhost:3838 where shiny is running.
For example you could have the main location (which is simply / pointing to a specific folder) and changed the rstudio.conf to handle http://<web_server_ip>/rstudio
EDIT
where would i configure to redirect my rstudio login to
If you want the rstudio login page to be accessible from http://<server>/rtudio (for example) you would need to change in the `/etc/nginx/RStudioAMI/rstudio.conf``
location /rstudio/ {
proxy_pass http://localhost:8787/;
proxy_redirect http://localhost:8787/ $scheme://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
access_log /var/log/nginx/rstudio-access.log;
error_log /var/log/nginx/rstudio-error.log;
}
If you want to point the main http://<server>/index.html pointing to /home/idx/index.html you need to change in /etc/nginx/sites-enabled/RStudioAMI.conf and have a main location defined pointing to your root element
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80 default_server;
index index.html;
location = / {
root /var/www/html;
}
include /etc/nginx/RStudioAMI/*.conf;
}
Note: Anytime you make a change to a nginx conf file, you need to restart nginx.
with: /etc/init.d/nginx restart.

Resources