0

I'm trying to serve both an API and frontend static site from the same EC2 instance. I'm using nginx and my server block as follows with the API being in port 8001 and static files to be served to /sub/:

 server_name abc.def.com www.abc.def.com; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://localhost:8001/; } location /sub/ { alias /var/www/subweb/; index index.html; } 

index.html

<script src="/vendor.aea0bbda027e3c98f5c1.js"></script> 

However what happens is my index.html tries to fetch the resources (javascript file) from root instead of /sub/.

How can I fix that so that it points to the right location?

*UPDATE - as advised, I have switched the order of locations but it's still not working as intended:

server_name abc.def.com www.abc.def.com; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location /sub/ { alias /var/www/subweb/; index index.html; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://localhost:8001/; } 
6
  • Fix the broken URL? Commented Feb 23, 2019 at 14:17
  • Thanks for the reply, but I don't think that's ideal as it will mean changing every single link on the site. Commented Feb 23, 2019 at 15:18
  • So if your URLs are not in /sub/ then why do you expect that location to apply to them? Commented Feb 23, 2019 at 15:24
  • they are relative links, just like the script. I would assume the behaviour would be the same as above. Commented Feb 23, 2019 at 15:31
  • I should add that It's a React site. Maybe that's what's causing the confusion Commented Feb 23, 2019 at 15:38

1 Answer 1

0

Change the order of locations.

2
  • Oh, this surely does. Commented Feb 23, 2019 at 14:33
  • Thanks for the reply. I have updated as per your advice but the outcome is the same. Commented Feb 23, 2019 at 15:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.