I run my nginx reverse proxy in a server over ssl with the domain: mydomain.com
.
This is below the nginx config:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream myApp {
server my-app:8080;
}
server {
listen 80;
server_name mydomain.com;
root /usr/share/nginx/html;
return 301 https://$host$request_uri;
}
server{
listen 443 ssl;
root /usr/share/nginx/html;
server_name mydomain.com;
include /etc/nginx/mime.types;
ssl_protocols TLSv1.2 TLSv1.3 TLSv1.1;
ssl_certificate /etc/nginx/certs/myapp_chain.pem;
ssl_certificate_key /etc/nginx/certs/private.pem;
proxy_set_header X-Forwarded-For $proxy_protocol_addr; # To forward the original client's IP address
proxy_set_header X-Forwarded-Proto $scheme; # to forward the original protocol (HTTP or HTTPS)
proxy_set_header Host $host; # to forward the original host requested by the client
proxy_buffers 4 256k;
proxy_buffer_size 128k;
proxy_busy_buffers_size 256k;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
location /portal {
proxy_pass http://myApp/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
When I work locally, the app works well. And when I run it also with a different port (but not behind nginx) it is accessible well.
The issue is when I access it with mydomain.com/portal
, I get this error (in picture). I allowed CORS btw.
And the console in the browser shows:
I figured it out. There are two places we should make changes to take into consideration the /portal sub-path.
At the index.html in the Angular project (webapps in case of JHipster)
<base href="/portal/" />
And in the nginx configuration file, we should add this line:
rewrite ^/portal(/.*)$ $1 break;
to remove the "/portal"
prefix from the request URI before forwarding the request to the myApp service.