NGINX + Vue HTML模式配置提供404

时间:2018-11-25 08:08:28

标签: nginx vue.js vue-router

我有一个nginx:alpine容器中运行的Vue应用,该容器具有自定义的nginx配置,以处理浏览器导航(Vue Router的html模式)。

问题是除根(/)之外的任何其他路径都给出了404,并带有以下错误消息:

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"

我正在使用如下所示的自定义nginx配置文件:

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;
        }

    }

}

我的nginx.conf有问题吗?

1 个答案:

答案 0 :(得分:1)

关于using if inside a location block的问题。

选择try_files块来处理请求时,不会执行if ($request_method = 'GET')语句。

您可以通过将try_files语句替换为另一个if语句来解决问题。

例如:

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