【问题标题】:nginx throws a 404 for files that exist on the pathnginx 为路径上存在的文件抛出 404
【发布时间】:2015-11-19 20:41:31
【问题描述】:

我在使用 nginx 时遇到了一个奇怪的问题。我使用的是 1.8.0 版本并且使用网络服务器地址我可以成功地从网络服务器地址提供文件,但不能从网站地址!

网络服务器地址:aoaapls00109la.stg-example.com 网址:stg.showdb.vod.example.com

我有一个这样的服务器块设置:

  server {
        listen       80;
        server_name  aoaapls00109la.stg-example.com stg.showdb.vod.example.com stg.showdb.vod;

        location / {
            add_header Access-Control-Allow-Origin "*";
            root   /opt/www/webserver/nginx/html;
            index  index.html index.htm;
            access_log  logs/stg.showdb.vod.access.log;
            error_log  logs/stg.showdb.vod.error.log;
        }

        location  ^/videos/ {
            root   /opt/www/webserver/nginx/html/videos;
            index  index.html index.htm;
            error_log  logs/videos_error.log;
        }
    }

我在“视频”位置下方有一个包含文件的目录。 NGINX 以名为“showdb”的用户身份运行,因此文件归该用户所有:

[root@aoaapls00109la ~]# ls -ld /opt/www/webserver/nginx/html/videos
drwxr-xr-x. 3 showdb showdb 4096 Nov 18 13:25 /opt/www/webserver/nginx/html/videos

     [root@aoaapls00109la ~]# ls -lh /opt/www/webserver/nginx/html/videos
total 2.8G
-rw-rw-r--. 1 showdb showdb 738M May 12  2015 1681_Scarlett_Johansson_Repeat.mp4
-rw-rw-r--. 1 showdb showdb  30K May 11  2015 200X200.jpg
-rw-r--r--. 1 showdb showdb  260 Nov 18 10:44 crossdomain.xml
-rw-rw-r--. 1 showdb showdb 238M May 12  2015 some_guy_TS_0260-05122015.mp4
-rw-r--r--. 1 showdb showdb   63 Jun  3 07:10 index.html
-rw-rw-r--. 1 showdb showdb 302M May 12  2015 LNWSM_101314_SHOW0112_med_clean.mp4
-rw-rw-r--. 1 showdb showdb 864M May  5  2015 LNWSM_101314_SHOW0112.mp4
-rw-rw-r--. 1 showdb showdb 331M May 12  2015 LNWSM_101314_SHOW0112_sm_clean.mp4
-rw-rw-r--. 1 showdb showdb  23K May 15  2015 some_guy_logo.png
-rw-rw-r--. 1 showdb showdb 391M May 15  2015 RK119_EP_0514-15.mp4
-rw-rw-r--. 1 showdb showdb  24K May  8  2015 SNL_40_COMPANY_16x9_1_SNL.png
-rw-rw-r--. 1 showdb showdb 160K May 12  2015 TSJF_LOGO.png
drwxrwxr-x. 5 showdb showdb 4.0K Nov 19 15:17 webvtt_files

我注意到,如果我将第一个 dns 名称用于 Web 服务器本身,我可以成功导航并查看“视频”文件夹的内容。

使用 curl 进行演示,我可以使用 Web 服务器名称地址从视频目录访问文件:

#curl http://aoaapls00109la.stg-example.com/videos/index.html
Video Store located on the server aoaapls00109la.stg-example.com

但是如果我使用网站地址,而不是像上面那样访问索引文件,我会得到 404!!

 curl http://stg.showdb.vod.example.com/videos/index.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

“videos”目录中的所有文件都会发生相同的行为。更糟糕的是,未提供服务且显示 404 的文件不会在访问或错误日志中生成任何活动!

如果我在浏览器的一个选项卡中从 Web 服务器地址调出相同的文件,我可以看到相同的位置为在 Web 服务器地址下成功显示的相同文件提供 404。

这是整个 nginx 配置,以防万一可能有一些关于为什么会发生这种情况的线索:

#user  nobody;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
#
user showdb;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log;

        location / {
            root   html;
            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   html;
        }



    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listen       80;
        server_name  aoaapls00109la.stg-example.com stg.showdb.vod.example.com stg.showdb.vod;

        location / {
            add_header Access-Control-Allow-Origin "*";
            root   /opt/www/webserver/nginx/html;
            index  index.html index.htm;
            access_log  logs/stg.showdb.vod.access.log;
            error_log  logs/stg.showdb.vod.error.log;
        }

        location  ^/videos/ {
            root   /opt/www/webserver/nginx/html/videos;
            index  index.html index.htm;
            error_log  logs/videos_error.log;
        }
    }


    server {
        listen       80;
        server_name   stg.cdn.showsys.example.com stg.cdn.showsystem.example.com;
        add_header Access-Control-Allow-Origin "*";
          location / {
            root   /opt/www/webserver/nginx/html/videos;
            index  index.html index.htm;
            access_log  logs/stg.cdn.showsys.access.log;
            error_log  logs/videos_error.log;
        }
    }

server {
        listen       443 ssl;
        server_name   stg.showdb.vod.example.com stg.showdb.vod;
        ssl_certificate     /etc/pki/tls/certs/aoaapls00109la.stg-example.com.crt;
        ssl_certificate_key /etc/pki/tls/private/aoaapls00109la.stg-example.com.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        location / {
            add_header Access-Control-Allow-Origin "*";
            root   /opt/www/webserver/nginx/html;
            index  index.html index.htm;
            access_log  logs/stg.showdb.vod.access.log;
            error_log  logs/stg.showdb.vod.error.log;
        }

        location /videos/ {
            root   /opt/www/webserver/nginx/html/videos;
            index  index.html index.htm;
            error_log  logs/videos_error.log;
        }
    }

server {
        listen       443 ssl;
        server_name  stg.cdn.showsys.example.com stg.cdn.showsystem.example.com;
        ssl_certificate     /etc/pki/tls/certs/aoaapls00109la.stg-example.com.crt;
        ssl_certificate_key /etc/pki/tls/private/aoaapls00109la.stg-example.com.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        add_header Access-Control-Allow-Origin "*";

          location / {
            root   /opt/www/webserver/nginx/html/videos;
            index  index.html index.htm;
            access_log  logs/stg.cdn.showsys.access.log;
            error_log  logs/videos_error.log;
        }
    }

这太莫名其妙了!!我究竟做错了什么?我该如何解决这个问题?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    如果您想要 RegEx 匹配,请不要忘记您的波浪号(或 ~* 用于不区分大小写的 RegEx)。正则表达式匹配胜过非正则表达式匹配

    location ~  ^/videos/ {
      ...
    }
    

    如果您不打算进行 RegEx 匹配,请删除波浪号

    location /videos/ {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2013-12-09
      • 1970-01-01
      • 2018-11-26
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多