0

I want to deploy my react app's in nginx under the same server, serving different html's for different locations, I have tried below configuration but it din't work for me

 server { listen 8080; # root /usr/share/nginx/build-bms; # Add index.php to the list if you are using PHP server_name _; location ^~ /portal1/ { root PATH_TO_BUILD_FOLDER_ONE; try_files $uri /index.html; index invalid; add_header Cache-Control public; expires 1d; autoindex on; } location ^~ /portal2/ { root PATH_TO_BUILD_FOLDER_TWO; try_files $uri /index.html; add_header Cache-Control public; index invalid; expires 1d; break; autoindex on; } location / { root PATH_TO_BUILD_FOLDER_THREE; try_files $uri /index.html; add_header Cache-Control public; expires 1d; } } 

Every time only the last one is served

3
  • If the folders are called portal1 and portal2, your root statement needs to point to the directory above. The path to the file is formed by concatenating the value of root with the requested URI. See this document. Commented Apr 29, 2020 at 19:18
  • actually folder name and and paths are different Commented Apr 29, 2020 at 19:18
  • You may need to use alias and you may want to avoid using try_files. See this answer but ignore the bits about PHP. Commented Apr 29, 2020 at 19:36

1 Answer 1

0

Without being a specialist on nginx i checked my config and would suggest to remove the regex match (if you don't need it) and transform your config to:

server { listen 8080; root /usr/share/nginx/build-bms; # Add index.php to the list if you are using PHP server_name _; location ^~ /portal1/ { alias /usr/share/nginx/portal1/; try_files $uri /index.html index.php; index invalid; add_header Cache-Control public; expires 1d; autoindex on; } location ^~ /portal2/ { alias /usr/share/nginx/portal2/; try_files $uri /index.html index.php; add_header Cache-Control public; index invalid; expires 1d; break; autoindex on; } location / { alias /usr/share/nginx/portal3/; try_files $uri /index.html index.php; add_header Cache-Control public; expires 1d; } } 

Instead of using multiple roots i would use alias. But I would always specify a root element for the server and then work with aliases. I would leave that up to the nginx pros to comment.

5
  • tried it, It din't work Commented Apr 29, 2020 at 19:38
  • I have edited my answer. It works now. It looks like there is a problem with the try_files directive. when you change try_files $uri /index.html; to try_files $uri /index.html index.php; it works. I am not 100% sure why to be honest but it works. Commented Apr 29, 2020 at 21:13
  • I am not using php over here Commented Apr 30, 2020 at 5:14
  • i was not using php either but as soon as you add this your config works. What I wanted to say is that your try_files directive doesn’t work. Try this try_files $uri $uri/ /index.html; Commented Apr 30, 2020 at 10:56
  • When i try that try files directive, html is served correctly but assets like js files are being served Commented Apr 30, 2020 at 12:36

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.