Reverse proxy for react contaiener - reactjs

I have 2 running docker containers. N1 with react app, N2 with nginx for reverse proxy.
Docker command
docker run -d --name app3 -p 3000:3000 testApp
started react container, and now it is accessible on 192.168.1.103:3000
(host is linux server on local network)
react app is test app and it opens fine on port 3000.
Now I want to open it using
192.168.1.103/app3
For that in Nginx container I prepared config
location /app3/ {
proxy_pass http://192.168.1.103:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Then I've restarted nginx and now when I open 192.168.1.103/app3 I see white page and if I inspect it I see in body You need to enable JavaScript to run this app.
But my browser is OK because second ago I opened the same app on port 3000 and it opened perfectly.
How can I fix this problem ?

Maybe you can docker-compose in same docker network or when you running both of your app you can running in same docker network.
Than after that in your nginx config you can make this:
location /app3/ {
rewrite /app3/(.*) /$1 break;
proxy_pass http://app3:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
And don't forget to expose port 80 and 443 when you running nginx container.

Related

How to host multiple create-react-app development servers under nginx with working live (hot) reload

I am developing a website with React.js for the frontend and have 2 separate apps for the users and the admins. The users will be under example.com and the admins under example.com/admin.
I am developing both apps behind an nginx server as a reverse proxy. I have had no issue developing a single app behind nginx, but I cannot use hot reload for the 2nd app. The app is served properly, with the only exception that the hot reload does not work.
I have HTTPS=true on both my .env files of the React.js apps. The main app's hot reload works fine, but the /admin app's hot reload fails with the error Firefox can’t establish a connection to the server at wss://192.168.1.2/adminws (developing through local network, so I can test the apps on my phone as well, but the hot reload won't work on the localhost either).
The main app is hosted under port 3000, the admin app is hosted under port 4000.
This is what my main app's .env looks like:
HTTPS=true
WDS_SOCKET_PORT=443
FAST_REFRESH=true
This is what my admin app's .env looks like:
HTTPS=true
WDS_SOCKET_PORT=443
WDS_SOCKET_PATH=/adminws
FAST_REFRESH=true
This is what my nginx configuration file looks like:
server {
# listen 80 default_server;
# listen [::]:80 default_server;
# SSL configuration
#
listen 443 ssl http2 default_server;
listen [::]:443 ssl default_server;
ssl on;
ssl_certificate /etc/nginx/ssl/localhost.crt;
ssl_certificate_key /etc/nginx/ssl/localhost.key;
gzip on;
gzip_types text/plain application/xml application/json;
gzip_proxied any;
gzip_min_length 1000;
gunzip on;
gzip_static on;
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 _;
location /ws {
proxy_pass https://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
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 /adminws {
proxy_pass https://127.0.0.1:4000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
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 /api {
proxy_pass http://127.0.0.1:3200;
}
location /admin {
proxy_pass https://127.0.0.1:4000;
}
location / {
proxy_pass https://127.0.0.1:3000;
}
}
I should note that the admin app's hot reload works properly when I remove both WDS_SOCKET_PORT and WDS_SOCKET_PATH from the .env file and run it on https://localhost:4000/admin, but this way I would not be able to test it behind nginx.
I removed both WDS_SOCKET_PORT and WDS_SOCKET_PATH from the admin app's .env file and it now seems to be working properly. Everything else seems to be ok.

Serving React application with relative path

I'm having two React applications running on the same machine. I'm using an nginx reverse proxy to handle routing requests.
The first application is running on root: '/', here is the nginx rule for it:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Proto https; # for ssl
}
The other app is running on port 4000, using the following reverse proxy:
location /staging/ {
proxy_pass http://127.0.0.1:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Proto https; # for ssl
}
I followed the [documentation][1] for deployment and I added the following to my package.js:
"homepage": "https:/xyz.com/staging/"
The application works using npm start. However using:
npm run build && serve -s build -l 4000
fails to load.
The request to index.html is fine however, every time the app tries to request the static content, it always responds with the index.html contents:
[![enter image description here][2]][2]
I tried setting PUBLIC_URL as well, but also failed.
Why is react failing to respond with any thing in the static directory?
Any help appreciated.
[1]: https://create-react-app.dev/docs/deployment/
[2]: https://i.stack.imgur.com/ijqx9.png

docker React, django(gunicorn), nginx reverse proxy with https give Bad request 400 on accessing backend APIs

I have containerized application with following containers working together with docker-compose
Nginx - reverse proxy
React - frontend
Django served using gunicorn - backend
When using http all works well with frontend and backend. I used letsencrypt certbot to generate ssl certificates. On switching to https, fronend seems to work fine however none of backend apis are working. Any request to backend such as login generates 'Bad Request 400' response.
I have tried following options but none worked.
a. Switching Django to production and adding ALLOWED_HOSTS=['*'] or specific domain
b. Adding upstream in nginx.conf
c. disableHostCheck: true in webpack devServer.
Following are my docker-compose and nginx.conf respectively
docker-compose.yml
services:
reverse_proxy:
image: nginx:1.17.10
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
- ./reverse_proxy/certbot-etc:/etc/letsencrypt
- ./reverse_proxy/certbot-var:/var/lib/letsencrypt
ports:
- 80:80
- 443:443
depends_on:
- webapp
- frontend
webapp:
build : ./backend/
command: gunicorn --bind 0.0.0.0:8000 ai.wsgi:application
expose:
- 8000
volumes:
- ./backend/:/app
ports:
- 8000:8000
frontend:
build: ./frontend/ai-web/
command: npm run ai
ports:
- 3000:3000
nginx.conf
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
}
http {
fastcgi_read_timeout 3000;
proxy_read_timeout 3000;
client_max_body_size 1000M;
upstream django {
server webapp:8000;
}
server {
listen 80;
server_name localhost 127.0.0.1;
location / {
proxy_pass http://frontend:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
}
location /api/ {
proxy_pass http://webapp:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 443 ssl;
server_name my_domain;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://frontend:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto "https";
}
location /api/ {
proxy_pass http://webapp:8000;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto "https";
}
ssl_certificate /etc/letsencrypt/live/my_domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/my_domain/privkey.pem;
}
}
Am I missing opening container port while using https ?
Please provide any pointers.

setting up nginx and two sails app on a server

I have two different application one acts as a backend panel whereas one app is client facing site.
I have set the client site on the default 80 port and backend panel app is on say port number 8000.
I am facing issue when i try to bind wwww.abc.com:8000 to www.abc.com/admin. On opening of the link, text do gets displayed but css and js is not getting loaded. I found in console that its says CSS and javascript files are not found. Also, the address it specifies for css and javascript is of the root folder and not of the backend panel folder.
Here is the code I have written to setup nginx to bind the route is here
server {
listen 80;
server_name abc.com
include /etc/nginx/mime.types;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:1337;
}
location ^~ /admin {
root /root/workspace/lexcarts-admin-panel;
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://127.0.0.1:8000;
proxy_redirect on;
}
}
Client app is accessible from default www.abc.com .
Please guide me where i am going wrong.
TIA

I have a proxied node app on port 9000 working well, but after login the port shows up why?

My angular-fullstack express app is working well. It is served on port 9000 through nginx using proxy_pass https://example.com:80 -> https://example.com:9000. When a new user looks to https://example.com they are routed to https://example.com/login as expect. However, once they login, whey see the url: https://example.com:9000 on firefox. And on Chrome they see: https://example.com:9000/# ??? why in the world is the port showing up and why is the # appearing?
In case the nginx settings are to blame for the proxy_pass, here they are:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass https://127.0.0.1:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
Is there a simple way to tell nginx to hide the port on the response, and why does the # appear on Chrome?
Your insight is appreciated!
To address the port number showing up, you might need to set the proxy_redirect directive in your nginx config. As the linked doc states, proxy_redirect
Sets the text that should be changed in the “Location” and “Refresh” header fields of a proxied server response
which is most likely where the port number is being added in.
As for the #, that's application specific - you're going to have to look at your code to figure out why.

Resources