Docker Reverse Proxy to React App with React Router - reactjs

I've been hitting my head on the keyboard for 3 days trying to figure out why I can't get this to work.
Right now I am running two nginx servers on my machine. Both are running in docker containers.
One nginx is a reverse proxy. The other nginx is the actual one serving the static files built by 'yarn build'.
I currently get a 404 error when trying to access any of the files out side of the root file.
So example.com works just fine. But when I go to example.com/test (a react route) I get a 404 error.
So, here is my reverse proxy confiruation.
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://192.168.1.29:8080;
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 ^~ /.well-known/acme-challenge/ {
root /var/www/html;
}
}
Here is my actual example.com confiration to serve the static files (yes it's port 80 because it's running on port 80 inside the docker container. The exposed docker container port is 8080:
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html;
try_files $uri $uri/ index.html;
index index.html;
location / {
}
}

Related

I am trying to serve react app from nginx inside docker container and container is mapped to a server port which is using nginx for serving multi app

I created the build of react app and using it to build docker image and serving a build app through "nginx" to port 80 after that I'm running the image in an ubuntu server and in that server there is also nginx server which is serving multiple app but it is not running the application on server
Note: The same process when I tried in my local system the application is running fine on localhost:17000 and there is no nginx installed in my system. So, I am assuming that something is missing in server nginx config file.
Server nginx version: nginx/1.18.0 (Ubuntu 20.04.2 LTS)
Docker nginx version: nginx/1.17.8 (alpine)
Dockerfile
FROM nginx:1.17.8-alpine
COPY build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
nginx.conf file in application which is copied during docker image building
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
# to redirect all the requests to index.html,
# useful when you are using react-router
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
docker container command
docker run -d -p 17000:80 --name container-name docker_image
Server nginx.conf file
server {
listen 80;
listen [::]:80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name domain.com;
ssl_certificate /home/certs/new_certs/bundle.crt;
ssl_certificate_key /home/certs/new_certs/privkey.pem;
# ssl_certificate /home/certs/chain.crt;
# ssl_certificate_key /home/certs/pk.pem;
access_log /var/log/nginx/access_log combined;
location /report.html {
root /var/www/html;
#report.html
}
location ^~ /app-platform/api/ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://ip_address:15000/";
}
location /app1 {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://ip_address:7006";
auth_basic "Private Property";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /app2 {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://ip_address:7007";
auth_basic "Private Property";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /app3 {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://ip_address:17000";
auth_basic "Private Property";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
When I inspect the source tab whole build files are not coming only one to two files are coming

How to set up a react + express app on ubuntu on a subdirectory

i'm trying to deploy a react app with express on a ubuntu server but I don't think i'm making much headway. I was able to get the entire front end(react) side working, but I can't seem to connect the client to the backend. I made sure to update the "homepage" in the package.json file before building the react app that it'll built with the subdirectory as the root. I have react router dom set up where when I access the url "tools.domain.com/style/{styleNumber}" then it makes a request to the express route "/getStyle".However, I receive a 405 error when trying to make that request. I have express running on port 6000. I'm not too sure if it's something I need to adjust in my nginx default.config file or possibly something in react/express? I've read that it has something to do with nginx not being able to make post requests through static files. Any help would be appreciated!
This is the error I am receiving when trying to make a request to the express route/endpoint:
https://i.stack.imgur.com/SdECo.jpg
Here is my default config file:
server {
listen 80;
root /var/www/home;
server_name tools.domain.com;
location / {
root /var/www/home;
try_files $uri $uri/ index.html;
}
location /app1 {
alias /var/www/app1;
try_files $uri $uri/ /app1/index.html;
}
location ^~ /app2 {
alias /var/www/app2;
try_files $uri $uri/ /app2/index.html;
}
location /getStyle/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header content-type "application/json";
proxy_cache_bypass $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_pass http://localhost:6000/;
}
}

how to check if my nginx server has x-frame disabled

I'm doing a port 80 redirect with namecheap : i'm doing mydomain.com to redirect to my server 400.300.200.100:myport. myport is not 80 but another port.
Now namecheap is stating "If the server (you are redirecting the domain to) has X-Frame feature disabled, you may select a Masked Redirect for the client's browser to display your domain name instead of http://1.2.3.4:50."
I would like my domain to be displayed instead of myserver:port. So where should i check if I have x frame disabled? in my react frontend ? in my nginx configuration?
Should I put
X-Frame-Options: DENY
or
X-Frame-Options: SAMEORIGIN
?
Can someone tell me if i need to configure this on nginx?
this is my nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/storybook-static;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /wagtail {
proxy_pass http://172.20.128.2:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Script-Name /wagtail;
}
location /static/ {
alias /app/static/;
}
location /media/ {
alias /app/media/;
}
}
For security reasons usually an xframe 'same origin' option is used.
Go to where Nginx is installed and then a conf folder
check the following parameter in nginx.conf under server section
add_header X-Frame-Options.
Another way is to go to your Nginx installation directory, if using Linux and run
grep -rnw 'X-Frame'
It will show you all files with header traces.

How to deploy NextJS with NGINX?

So I know how to deploy a React app on a server.
npm run build
create a server block and point the root to my react app folder build (root /var/www/xfolder/build;)
systemctl restart nginx
run my node server (nohup node server &&) and its done.
I feel kind of dumb for not understanding this with NextJS. I run npm run build
I'm expecting something like a build folder. I've tried setting the server block root to
/var/www/xfolder/.next but the page still gives 403 forbidden. And do I need to run npm run start? I'm confuse on how to properly deploy the app. I'm using Ubuntu, NginX (1gb droplet) in DigitalOcean.
Check this: https://gist.github.com/iam-hussain/2ecdb934a7362e979e3aa5a92b181153
Reference for HTTP/HTTPS: https://gist.github.com/iam-hussain/2ecdb934a7362e979e3aa5a92b181153
Start PM2 nextJS service on port 8080:
cd PROJECT_DIRECTORY
pm2 start "npm run start -- -p 8080" --name contractverifier
Configure Nginx:
Replace this file with the below code /etc/nginx/sites-available/default
server {
server_name www.DOMAINNAME.com DOMAINNAME.com;
index index.html index.htm;
root /home/ubuntu/PROJECT_FOLDER; #Make sure your using the full path
# Serve any static assets with NGINX
location /_next/static {
alias /home/ubuntu/PROJECT_FOLDER/.next/static;
add_header Cache-Control "public, max-age=3600, immutable";
}
location / {
try_files $uri.html $uri/index.html # only serve html files from this dir
#public
#nextjs;
add_header Cache-Control "public, max-age=3600";
}
location #public {
add_header Cache-Control "public, max-age=3600";
}
location #nextjs {
# reverse proxy for next server
proxy_pass http://localhost:8080; #Don't forget to update your port number
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;
}
listen 80 default_server;
listen [::]:80;
}
I managed to make it work. The problem is on my Nginx server block. I just add this block
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;
}
then run
npm start
I prefer to pm2 in order to start nextJs service and Nginx for publishing it
pm2 cmd:
pm2 start yarn --name nextjs --interpreter bash -- start
pm2 show nextjs
You can push that config into /etc/nginx/conf.d/your-file.config
/etc/nginx/nginx.config
server {
listen 80; # you can use 443 and letsencrypt to get SSL for free
server_name dicom-interactive.com; # domain name
access_log /var/log/dicom-interactive/access.log; # mkdir dir first
error_log /var/log/dicom-interactive/error.log error;
# for public asset into _next directory
location _next/ {
alias /srv/udemii-fe/.next/;
expires 30d;
access_log on;
}
location / {
# reverse proxy for next server
proxy_pass http://localhost:8000; # your nextJs service and port
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;
# we need to remove this 404 handling
# because next's _next folder and own handling
# try_files $uri $uri/ =404;
}
}

React app doesnt launch from url location as set by nginx reverse proxy but does when port explicitly set

I'm trying to run a single react app on a separate port and using proxy pass from nginx to location '/app'
"http://localhost/app" -> React Application
The application is running under PM2 using its generated build with the command: 'pm2 serve build 3002'.
My thought was i do all this tested locally and then will port it to my own domain.
I've swapped 'localhost' for '127.0.0.1' just in case that had anything to do with it.
From online tutorials i've ended up with the following in my nginx "../sites-available/my_domain" file
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /var/www/my_domain/html;
index index.html index.htm index.nginx-debian.html;
try_files $uri $uri/ =404;
}
location /app {
proxy_pass http://localhost:3002;
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;
}
}
If i type "http://localhost:3002" into the url it works fine however "http://localhost/app" does not work and shows 404 error.
Side-note: "http://localhost" works fine and directs to the index.html stored at root.

Resources