21

这是我的 nginx 配置文件。

在 default.conf 中,第一个位置是用来访问 /usr/share/nginx/html 目录的,我访问http://47.91.152.99就可以了。但是当我为目录 /usr/share/nginx/public 目录添加一个新位置时,当我访问http://47.91.152.99/test时,nginx 会返回一个 404 页面。

那么,到底是怎么回事?我是否滥用了 nginx 的指令?

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location ^~ /test/ {
        root /usr/share/nginx/public;
        index index.html index.htm;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
4

6 回答 6

31

以下错误块(在您的情况下);

 location ^~ /test/ {
     root /usr/share/nginx/public;
     index index.html index.htm;
 }

告诉 nginx 在文件夹(根)/usr/share/nginx/public 中查找目录“test”。如果该根目录中没有“test”文件夹,它将返回 404。为了解决您的问题,我建议您尝试使用alias而不是root。像这样:

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }

此外,只是为了踢球,通常可以设置索引指令,这样你就不必一直重写它......就像这样;

 server {
     listen       80;
     server_name  localhost;

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

     error_page   500 502 503 504  /50x.html;

     location / { }

     location ~^/test/ {
         alias /usr/share/nginx/public;
     }

     location = /50x.html {
         root   /usr/share/nginx/html;
     }
 }

您还应该考虑的一件事......位置块越“精确”,它应该驻留在配置中的位置就越高。像那样location = /50x.html。在一个完美的世界里,这将被设置在顶部,就在一般服务器块设置之后。

希望能帮助到你。

于 2016-12-12T12:59:50.803 回答
11

根指令引起的错误

location ^~ /test/ {
    root /usr/share/nginx/public;
    index index.html index.htm;
}

使用别名指令修复

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }

其他改进

额外提示:可以设置 index 指令,这样您就不必重新编写它。

server {
    listen       80;
    server_name  localhost;

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

    error_page   500 502 503 504  /50x.html;

    location / { }

    location ~^/test/ {
        alias /usr/share/nginx/public;
    }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

nginx 部分根据配置中的位置匹配位置块。理想情况下,你会颠倒你现在拥有的东西。位置块在 nginx 配置中会更高。为此, location = /50x.html 也会向上移动。订单是

  1. 完全匹配 =
  2. 正向匹配^~/
  3. 区分大小写的正则表达式 ~ /
  4. 不区分大小写的正则表达式 ~*
  5. 路径匹配 /

有关nginx 位置优先级的更多信息。此外,您可以随时查看官方文档。位置块的 nginx 文档http://nginx.org/en/docs/http/ngx_http_core_module.html#location

于 2018-11-11T09:11:02.473 回答
1

当你的应用是vuejs时,你需要这样写,可以防止404,注意双/test/

   location ^~/test/ {
       alias /usr/local/soft/vuejs/;
       try_files $uri $uri/ /test/index.html;
    }
于 2020-06-22T13:36:41.223 回答
0

就我而言,如果您托管多个站点,请检查server_name它可能会被错误地指向。也看看那个。

于 2019-12-12T06:54:27.577 回答
0

和我的 server-blocks.conf

server {

        listen 80;

        index index.html index.htm index.nginx-debian.html;

        server_name 13.xxx.xxx.xx;

        location / {
        root /var/www/portaladmin/;


                proxy_pass http://13.xxx.xxx.xx:80/;

        }
         error_log /var/log/nginx/portaladmin-erorr.log;

}

和我的 load-balancer.conf

server {
  listen 80;
  server_name xx.xxx.xxx.xx
  access_log /home/ec2-user/logs/lb-access.log;
  error_log /home/ec2-user/logs/lb-error.log;

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://13.xxx.xxx.xx:80;
           }
}
于 2021-10-12T04:23:23.420 回答
0

我刚刚解决了这个(index.html not found)问题。
对我来说,我输错了我的项目名称以匹配您的 ec2 项目名称和 nginx 路径。

移动到 nginx/sites-enabled 以检查 nginx 路径

  1. cd /etc/nginx/sites-enabled
  2. cat你的项目名称
  3. 检查你的 nginx 路径(对我来说:root/home/ubuntu/ practice /current/public;)

移动到主目录以检查您的项目名称
4. cd
5. ls
6. 如果您的 ec2 项目名称(对我而言:练习)与您的 nginx 路径名称(对我而言:练习)不匹配,那么您可能会得到“index.html未找到错误”

于 2021-10-10T09:02:18.757 回答