来自源的 Nginx,返回 403 Forbidden

来自源的 Nginx,返回 403 Forbidden

我在虚拟机上安装的 Ubuntu 19.10 上使用源代码安装了 nginx。iptables 已禁用。selinux 已禁用。firewalld 已禁用。我以这种方式启动服务器:

./root/nginx-1.16.1/objs/nginx

服务器运行顺利。但如果我尝试访问 IP 地址,则会得到:

 wget 127.0.0.1:443
--2019-10-30 07:41:27--  http://127.0.0.1:443/
Connecting to 127.0.0.1:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2019-10-30 07:41:27 ERROR 400: Bad Request.

wget 192.168.136.133:443
--2019-10-30 07:42:00--  http://192.168.136.133:443/
Connecting to 192.168.136.133:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2019-10-30 07:42:00 ERROR 400: Bad Request.

错误日志我懂了:

2019/10/30 07:34:59 [error] 1325#0: *1 "/root/nginx-1.16.1/html/index.html" is forbidden (13: Permission denied), client: 192.168.136.1, server: localhost, request: "GET / HTTP/2.0", host: "192.168.136.133"
2019/10/30 07:41:18 [error] 1325#0: *2 "/root/nginx-1.16.1/html/index.html" is forbidden (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1"

您对可能出现的问题有什么想法吗?

详情请见: nginx.conf

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


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;
     error_log  logs/error.log debug;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {

    ########################################################
    ########################################################
        # Enable QUIC and HTTP/3.
        listen 443 quic reuseport;

        # Enable HTTP/2 (optional).
        listen 443 ssl http2;

        ssl_certificate      cert.crt;
        ssl_certificate_key  cert.key;

        # Enable all TLS versions (TLSv1.3 is required for QUIC).
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

        # Add Alt-Svc header to negotiate HTTP/3.
        add_header alt-svc 'h3-23=":443"; ma=86400';
    ########################################################
    ########################################################


        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}



    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

允许:

ll
total 812
drwxr-xr-x 15   1001 1001   4096 Oct 29 20:49 ./
drwx------ 14 root   root   4096 Oct 30 09:49 ../
drwxr-xr-x  6   1001 1001   4096 Oct 29 18:35 auto/
-rw-r--r--  1   1001 1001 296463 Aug 13 14:51 CHANGES
-rw-r--r--  1   1001 1001 452171 Aug 13 14:51 CHANGES.ru
drwx------  2 nobody root   4096 Oct 29 20:49 client_body_temp/
drwxr-xr-x  2   1001 1001   4096 Oct 29 20:49 conf/
-rwxr-xr-x  1   1001 1001   2502 Aug 13 14:51 configure*
drwxr-xr-x  4   1001 1001   4096 Oct 29 18:32 contrib/
drwx------  2 nobody root   4096 Oct 29 20:49 fastcgi_temp/
drwxrwxrwx  2   1001 1001   4096 Oct 29 18:32 html/
-rw-r--r--  1   1001 1001   1397 Aug 13 14:51 LICENSE
drwxr-xr-x  2 root   root   4096 Oct 30 09:47 logs/
-rw-r--r--  1 root   root    384 Oct 29 18:37 Makefile
drwxr-xr-x  2   1001 1001   4096 Oct 29 18:32 man/
drwxr-xr-x  3 root   root   4096 Oct 29 19:00 objs/
drwx------  2 nobody root   4096 Oct 29 20:49 proxy_temp/
-rw-r--r--  1   1001 1001     49 Aug 13 14:51 README
drwx------  2 nobody root   4096 Oct 29 20:49 scgi_temp/
drwxr-xr-x  9   1001 1001   4096 Oct 29 18:32 src/
drwx------  2 nobody root   4096 Oct 29 20:49 uwsgi_temp/

您对可能出现的问题有什么想法吗?

答案1

这通常发生在运行工作进程的用户/组nginx(在常规安装中www-data)没有权限访问/读取用于提供响应的文件时。这可能是因为用户没有足够的权限遍历目录,也可能是因为他们根本没有读取文件的权限。

当你手动启动nginx服务器时,你需要确保调用用户(运行 的用户./root/nginx-1.16.1/objs/nginx)确实有权读取/root/nginx-1.16.1/html/index.html。你可以从终端ls以该用户身份运行,轻松检查这一点:

ls /root/nginx-1.16.1/html/index.html

那么这主要是权限修复问题。

此外,只要有可能,请坚持使用 Ubuntu 的官方软件包,除非您有意自己编译二进制文件,也许是为了启用官方版本中缺少的编译时选项。如果是这种情况,您也可以在发射台针对该软件包并与维护人员讨论启用该选项,也许他们可以在讨论之后添加该选项。

答案2

解决方案:

在文件开头conf/nginx.conf

我设置它:

user  root;

相关内容