I am trying to achieve the below with nginx -
I have 2 docker containers running on a server once container runs nginx on port 80 & takes requests from AWS application load balancer. Then based on the path in the URL, it is supposed to redirect to one of 3 ports on the other docker container. The applications running in the second docker container serve content on their own paths.
Example - When I type https://example.com/story/fairy
, Nginx is supposed to parse /story/fairy
& pass it to the other docker container on port 9091. The returned data from the app may be on path /_myownpath/page1/
. The browser URL at the end of it should look - https://example.com/story/fairy/_myownpath/page1
.
Then if there are other links on this returned page & the user clicks on them, nginx should pass just the new path to the app listening on port 9091 & say the new content returned will be on path /_newpath/story_page_11
. The browser URL should now look https://example.com/story/fairy/_newpath/story_page_11
& so on.
I've pulled my hairs apart playing with nginx config & haven't been able to get my head around and get this right.
Some of the configs I've tried are
Tried proxy pass
server {
listen 80;
server_name example.com;
location /story/fairy/ {
# Reject requests with unsupported HTTP method
if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
return 405;
}
# Only requests matching the whitelist expectations will
# get sent to the application server
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:9091/;
proxy_redirect http://localhost:9091/ https://$server_name/;
}
}
Tried redirecting
server {
listen 80;
location /awsmap/dev/ {
if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
return 405;
}
return 301 http://localhost:9091/;
}
Tried re-writing
server {
listen 80;
location /story/fairy {
rewrite ^/story(.*)$ / break;
proxy_pass http://localhost:9091;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
absolute_redirect off;
}
location ^~ /story\/fairy/ {
rewrite ^/story(.*)$ / break;
proxy_pass http://localhost:9091;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
absolute_redirect off;
}
}
I've got very little experience with nginx. Stuck at this point getting this to work.
I'd truly appreciate ay help to get this working please.