Redirection cycle while using alias












1















I'm trying to setup a home web-server.



My folder structure is the following:



/home/pi/www/
├── homeGUI
│ ├── backend /* Contains an express server listening on 8080 */
│ ├── frontend /* Contains an Angular application */
│ │ ├── index.html
│ │ └── /* Other .js files */
│ └── scripts
└── index.html


while my site configuration is this one:



server{
listen 80;
listen [::]:80;

root /home/pi/www;

index index.html index.htm;

server_name _;

#intended to serve the angular application
location /homeGUI/ {
alias /home/pi/www/homeGUI/frontend/;
try_files $uri $uri/ /homeGUI/frontend/index.html; #this one is causing problems
}

#intended to serve the files in the www folder
location / {
try_files $uri $uri/ index.html;
}

#intended to serve the express server
location /homeGUI/api/ {
proxy_pass http://127.0.0.1:8080
}
}


The configuration above can serve all 3 contents(the static index.html, the angular application and the express server).



The problem I have is when i navigate to a route of the angular application directly (or by refreshing the page while on that route) (es: localhost/homeGUI/route1) this cause a redirect cycle because it tries to redirect to /homeGUI/frontend/index.html which then falls into the same location rule and cycle.



All I want to accomplish is redirect every request like /homeGUI/* to the /home/pi/www/homeGUI/frontend/index.html file with the exception of /homeGUI/api which should go to the express server on port 8080.



Any help is appreciated.
Thanks.










share|improve this question



























    1















    I'm trying to setup a home web-server.



    My folder structure is the following:



    /home/pi/www/
    ├── homeGUI
    │ ├── backend /* Contains an express server listening on 8080 */
    │ ├── frontend /* Contains an Angular application */
    │ │ ├── index.html
    │ │ └── /* Other .js files */
    │ └── scripts
    └── index.html


    while my site configuration is this one:



    server{
    listen 80;
    listen [::]:80;

    root /home/pi/www;

    index index.html index.htm;

    server_name _;

    #intended to serve the angular application
    location /homeGUI/ {
    alias /home/pi/www/homeGUI/frontend/;
    try_files $uri $uri/ /homeGUI/frontend/index.html; #this one is causing problems
    }

    #intended to serve the files in the www folder
    location / {
    try_files $uri $uri/ index.html;
    }

    #intended to serve the express server
    location /homeGUI/api/ {
    proxy_pass http://127.0.0.1:8080
    }
    }


    The configuration above can serve all 3 contents(the static index.html, the angular application and the express server).



    The problem I have is when i navigate to a route of the angular application directly (or by refreshing the page while on that route) (es: localhost/homeGUI/route1) this cause a redirect cycle because it tries to redirect to /homeGUI/frontend/index.html which then falls into the same location rule and cycle.



    All I want to accomplish is redirect every request like /homeGUI/* to the /home/pi/www/homeGUI/frontend/index.html file with the exception of /homeGUI/api which should go to the express server on port 8080.



    Any help is appreciated.
    Thanks.










    share|improve this question

























      1












      1








      1








      I'm trying to setup a home web-server.



      My folder structure is the following:



      /home/pi/www/
      ├── homeGUI
      │ ├── backend /* Contains an express server listening on 8080 */
      │ ├── frontend /* Contains an Angular application */
      │ │ ├── index.html
      │ │ └── /* Other .js files */
      │ └── scripts
      └── index.html


      while my site configuration is this one:



      server{
      listen 80;
      listen [::]:80;

      root /home/pi/www;

      index index.html index.htm;

      server_name _;

      #intended to serve the angular application
      location /homeGUI/ {
      alias /home/pi/www/homeGUI/frontend/;
      try_files $uri $uri/ /homeGUI/frontend/index.html; #this one is causing problems
      }

      #intended to serve the files in the www folder
      location / {
      try_files $uri $uri/ index.html;
      }

      #intended to serve the express server
      location /homeGUI/api/ {
      proxy_pass http://127.0.0.1:8080
      }
      }


      The configuration above can serve all 3 contents(the static index.html, the angular application and the express server).



      The problem I have is when i navigate to a route of the angular application directly (or by refreshing the page while on that route) (es: localhost/homeGUI/route1) this cause a redirect cycle because it tries to redirect to /homeGUI/frontend/index.html which then falls into the same location rule and cycle.



      All I want to accomplish is redirect every request like /homeGUI/* to the /home/pi/www/homeGUI/frontend/index.html file with the exception of /homeGUI/api which should go to the express server on port 8080.



      Any help is appreciated.
      Thanks.










      share|improve this question














      I'm trying to setup a home web-server.



      My folder structure is the following:



      /home/pi/www/
      ├── homeGUI
      │ ├── backend /* Contains an express server listening on 8080 */
      │ ├── frontend /* Contains an Angular application */
      │ │ ├── index.html
      │ │ └── /* Other .js files */
      │ └── scripts
      └── index.html


      while my site configuration is this one:



      server{
      listen 80;
      listen [::]:80;

      root /home/pi/www;

      index index.html index.htm;

      server_name _;

      #intended to serve the angular application
      location /homeGUI/ {
      alias /home/pi/www/homeGUI/frontend/;
      try_files $uri $uri/ /homeGUI/frontend/index.html; #this one is causing problems
      }

      #intended to serve the files in the www folder
      location / {
      try_files $uri $uri/ index.html;
      }

      #intended to serve the express server
      location /homeGUI/api/ {
      proxy_pass http://127.0.0.1:8080
      }
      }


      The configuration above can serve all 3 contents(the static index.html, the angular application and the express server).



      The problem I have is when i navigate to a route of the angular application directly (or by refreshing the page while on that route) (es: localhost/homeGUI/route1) this cause a redirect cycle because it tries to redirect to /homeGUI/frontend/index.html which then falls into the same location rule and cycle.



      All I want to accomplish is redirect every request like /homeGUI/* to the /home/pi/www/homeGUI/frontend/index.html file with the exception of /homeGUI/api which should go to the express server on port 8080.



      Any help is appreciated.
      Thanks.







      nginx






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 22 '18 at 20:08









      Luca RegazziLuca Regazzi

      1084




      1084






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The last element of your try_files statement should be the URI.



          The URI for the file at /home/pi/www/homeGUI/frontend/index.html is /homeGUI/index.html and not /homeGUI/frontend/index.html.



          See this document for details.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          try_files $uri $uri/ /homeGUI/index.html;
          }




          The above may work, but using alias and try_files in the same block can cause problems due to this issue.



          You can replace your try_files statement with default behaviour and an if block.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          if (!-e $request_filename) { rewrite ^ /homeGUI/index.html last; }
          }


          See this caution on the use of if.






          share|improve this answer
























          • Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

            – Luca Regazzi
            Dec 22 '18 at 21:31











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1386998%2fredirection-cycle-while-using-alias%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The last element of your try_files statement should be the URI.



          The URI for the file at /home/pi/www/homeGUI/frontend/index.html is /homeGUI/index.html and not /homeGUI/frontend/index.html.



          See this document for details.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          try_files $uri $uri/ /homeGUI/index.html;
          }




          The above may work, but using alias and try_files in the same block can cause problems due to this issue.



          You can replace your try_files statement with default behaviour and an if block.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          if (!-e $request_filename) { rewrite ^ /homeGUI/index.html last; }
          }


          See this caution on the use of if.






          share|improve this answer
























          • Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

            – Luca Regazzi
            Dec 22 '18 at 21:31
















          1














          The last element of your try_files statement should be the URI.



          The URI for the file at /home/pi/www/homeGUI/frontend/index.html is /homeGUI/index.html and not /homeGUI/frontend/index.html.



          See this document for details.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          try_files $uri $uri/ /homeGUI/index.html;
          }




          The above may work, but using alias and try_files in the same block can cause problems due to this issue.



          You can replace your try_files statement with default behaviour and an if block.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          if (!-e $request_filename) { rewrite ^ /homeGUI/index.html last; }
          }


          See this caution on the use of if.






          share|improve this answer
























          • Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

            – Luca Regazzi
            Dec 22 '18 at 21:31














          1












          1








          1







          The last element of your try_files statement should be the URI.



          The URI for the file at /home/pi/www/homeGUI/frontend/index.html is /homeGUI/index.html and not /homeGUI/frontend/index.html.



          See this document for details.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          try_files $uri $uri/ /homeGUI/index.html;
          }




          The above may work, but using alias and try_files in the same block can cause problems due to this issue.



          You can replace your try_files statement with default behaviour and an if block.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          if (!-e $request_filename) { rewrite ^ /homeGUI/index.html last; }
          }


          See this caution on the use of if.






          share|improve this answer













          The last element of your try_files statement should be the URI.



          The URI for the file at /home/pi/www/homeGUI/frontend/index.html is /homeGUI/index.html and not /homeGUI/frontend/index.html.



          See this document for details.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          try_files $uri $uri/ /homeGUI/index.html;
          }




          The above may work, but using alias and try_files in the same block can cause problems due to this issue.



          You can replace your try_files statement with default behaviour and an if block.



          For example:



          location /homeGUI/ {
          alias /home/pi/www/homeGUI/frontend/;
          if (!-e $request_filename) { rewrite ^ /homeGUI/index.html last; }
          }


          See this caution on the use of if.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 22 '18 at 21:21









          Richard SmithRichard Smith

          513148




          513148













          • Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

            – Luca Regazzi
            Dec 22 '18 at 21:31



















          • Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

            – Luca Regazzi
            Dec 22 '18 at 21:31

















          Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

          – Luca Regazzi
          Dec 22 '18 at 21:31





          Your first guess was right. By using try_files $uri $uri/ /homeGUI/index.html; I can directly load localhost/homeGUI/route1 and it works like it should.

          – Luca Regazzi
          Dec 22 '18 at 21:31


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1386998%2fredirection-cycle-while-using-alias%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Plaza Victoria

          Puebla de Zaragoza

          Musa