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

  • 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](http://nginx.org/en/docs/http/ngx_http_core_module.html#root). – Richard Smith Apr 29 '20 at 19:18
  • actually folder name and and paths are different – Sumanth Madishetty Apr 29 '20 at 19:18
  • You may need to use `alias` and you may want to avoid using `try_files`. See [this answer](https://serverfault.com/questions/828523/why-nginx-internal-redirect-is-not-happening/828579#828579) but ignore the bits about PHP. – Richard Smith Apr 29 '20 at 19:36

1 Answers1

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.

Chris
  • 21
  • 1
  • 4