NGINX + Vue HTML mode config giving 404












1















I have a Vue app running inside an nginx:alpine container with a custom nginx config to deal with browser navigation (Vue Router's html mode).



The problem is that any path other than the root (/) is giving 404's, with error messages like:



2018/11/25 07:56:13 [error] 7#7: *2 open() "/usr/share/nginx/html/home" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /home HTTP/1.1", host: "localhost:4000"


I'm using a custom nginx config file that looks like this:



worker_processes  1;

events {
worker_connections 1024;
}

http {

include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {

listen 80;
server_name localhost;

root /usr/share/nginx/html;
index index.html;

location / {

try_files $uri $uri/ /index.html;

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}

if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}

if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}

}

location ~* .(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

}


is there something wrong with my nginx.conf?










share|improve this question



























    1















    I have a Vue app running inside an nginx:alpine container with a custom nginx config to deal with browser navigation (Vue Router's html mode).



    The problem is that any path other than the root (/) is giving 404's, with error messages like:



    2018/11/25 07:56:13 [error] 7#7: *2 open() "/usr/share/nginx/html/home" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /home HTTP/1.1", host: "localhost:4000"


    I'm using a custom nginx config file that looks like this:



    worker_processes  1;

    events {
    worker_connections 1024;
    }

    http {

    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {

    listen 80;
    server_name localhost;

    root /usr/share/nginx/html;
    index index.html;

    location / {

    try_files $uri $uri/ /index.html;

    if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=utf-8';
    add_header 'Content-Length' 0;
    return 204;
    }

    if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }

    if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }

    }

    location ~* .(?:ico|css|js|gif|jpe?g|png)$ {
    # Some basic cache-control for static files to be sent to the browser
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }

    }

    }


    is there something wrong with my nginx.conf?










    share|improve this question

























      1












      1








      1








      I have a Vue app running inside an nginx:alpine container with a custom nginx config to deal with browser navigation (Vue Router's html mode).



      The problem is that any path other than the root (/) is giving 404's, with error messages like:



      2018/11/25 07:56:13 [error] 7#7: *2 open() "/usr/share/nginx/html/home" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /home HTTP/1.1", host: "localhost:4000"


      I'm using a custom nginx config file that looks like this:



      worker_processes  1;

      events {
      worker_connections 1024;
      }

      http {

      include mime.types;
      default_type application/octet-stream;
      sendfile on;
      keepalive_timeout 65;

      server {

      listen 80;
      server_name localhost;

      root /usr/share/nginx/html;
      index index.html;

      location / {

      try_files $uri $uri/ /index.html;

      if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain; charset=utf-8';
      add_header 'Content-Length' 0;
      return 204;
      }

      if ($request_method = 'POST') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      }

      if ($request_method = 'GET') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      }

      }

      location ~* .(?:ico|css|js|gif|jpe?g|png)$ {
      # Some basic cache-control for static files to be sent to the browser
      expires max;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
      }

      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
      root html;
      }

      }

      }


      is there something wrong with my nginx.conf?










      share|improve this question














      I have a Vue app running inside an nginx:alpine container with a custom nginx config to deal with browser navigation (Vue Router's html mode).



      The problem is that any path other than the root (/) is giving 404's, with error messages like:



      2018/11/25 07:56:13 [error] 7#7: *2 open() "/usr/share/nginx/html/home" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /home HTTP/1.1", host: "localhost:4000"


      I'm using a custom nginx config file that looks like this:



      worker_processes  1;

      events {
      worker_connections 1024;
      }

      http {

      include mime.types;
      default_type application/octet-stream;
      sendfile on;
      keepalive_timeout 65;

      server {

      listen 80;
      server_name localhost;

      root /usr/share/nginx/html;
      index index.html;

      location / {

      try_files $uri $uri/ /index.html;

      if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain; charset=utf-8';
      add_header 'Content-Length' 0;
      return 204;
      }

      if ($request_method = 'POST') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      }

      if ($request_method = 'GET') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      }

      }

      location ~* .(?:ico|css|js|gif|jpe?g|png)$ {
      # Some basic cache-control for static files to be sent to the browser
      expires max;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
      }

      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
      root html;
      }

      }

      }


      is there something wrong with my nginx.conf?







      nginx vue.js vue-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 8:08









      riccardo_92riccardo_92

      256




      256
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are issues regarding using if inside a location block.



          The try_files statement is not executed when the if ($request_method = 'GET') block is selected to process the request.



          You can fix the problem by replacing the try_files statement with another if statement.



          For example:



          location / {
          if (!-e $request_filename) { rewrite ^ /index.html last; }
          if ($request_method = 'OPTIONS') { ... }
          if ($request_method = 'POST') { ... }
          if ($request_method = 'GET') { ... }
          }





          share|improve this answer
























          • That did the trick!

            – riccardo_92
            Nov 25 '18 at 15:01











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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%2fstackoverflow.com%2fquestions%2f53465718%2fnginx-vue-html-mode-config-giving-404%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














          There are issues regarding using if inside a location block.



          The try_files statement is not executed when the if ($request_method = 'GET') block is selected to process the request.



          You can fix the problem by replacing the try_files statement with another if statement.



          For example:



          location / {
          if (!-e $request_filename) { rewrite ^ /index.html last; }
          if ($request_method = 'OPTIONS') { ... }
          if ($request_method = 'POST') { ... }
          if ($request_method = 'GET') { ... }
          }





          share|improve this answer
























          • That did the trick!

            – riccardo_92
            Nov 25 '18 at 15:01
















          1














          There are issues regarding using if inside a location block.



          The try_files statement is not executed when the if ($request_method = 'GET') block is selected to process the request.



          You can fix the problem by replacing the try_files statement with another if statement.



          For example:



          location / {
          if (!-e $request_filename) { rewrite ^ /index.html last; }
          if ($request_method = 'OPTIONS') { ... }
          if ($request_method = 'POST') { ... }
          if ($request_method = 'GET') { ... }
          }





          share|improve this answer
























          • That did the trick!

            – riccardo_92
            Nov 25 '18 at 15:01














          1












          1








          1







          There are issues regarding using if inside a location block.



          The try_files statement is not executed when the if ($request_method = 'GET') block is selected to process the request.



          You can fix the problem by replacing the try_files statement with another if statement.



          For example:



          location / {
          if (!-e $request_filename) { rewrite ^ /index.html last; }
          if ($request_method = 'OPTIONS') { ... }
          if ($request_method = 'POST') { ... }
          if ($request_method = 'GET') { ... }
          }





          share|improve this answer













          There are issues regarding using if inside a location block.



          The try_files statement is not executed when the if ($request_method = 'GET') block is selected to process the request.



          You can fix the problem by replacing the try_files statement with another if statement.



          For example:



          location / {
          if (!-e $request_filename) { rewrite ^ /index.html last; }
          if ($request_method = 'OPTIONS') { ... }
          if ($request_method = 'POST') { ... }
          if ($request_method = 'GET') { ... }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 11:34









          Richard SmithRichard Smith

          21.3k42541




          21.3k42541













          • That did the trick!

            – riccardo_92
            Nov 25 '18 at 15:01



















          • That did the trick!

            – riccardo_92
            Nov 25 '18 at 15:01

















          That did the trick!

          – riccardo_92
          Nov 25 '18 at 15:01





          That did the trick!

          – riccardo_92
          Nov 25 '18 at 15:01




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53465718%2fnginx-vue-html-mode-config-giving-404%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

          Wiesbaden

          Marschland

          To store a contact into the json file from server.js file using a class in NodeJS