在 CentOS 7 上出现 403 Forbidden 时如何正确设置 Nginx?

问题描述 投票:0回答:2

在 CentOS 7 上

/etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.1  app1

从包中安装 Nginx:

yum install nginx

/etc/nginx/nginx.conf

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

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

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

/etc/nginx/sites-available/
下创建了一个名为
myapp
的新文件:

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:///home/deploy/myapp/tmp/sockets/unicorn.sock;
}

server {
    listen 192.168.0.1:80;
    server_name app1;

    # Application root, as defined previously
    root /home/deploy/myapp/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

将其链接到

/etc/nginx/sites-enabled/

cd /etc/nginx/sites-enabled/
ln -s ../sites-available/myapp

重启nginx:

service nginx restart

然后尝试访问url:

curl 192.168.0.1

出现错误:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>

我删除了

index.html
路径下的默认
/usr/share/nginx/html
文件,所以它得到了 403 Forbidden。

Nginx 错误日志

/var/log/nginx/error.log
:

2017/07/25 03:35:59 [error] 8200#0: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.0.2, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1"

为什么访问的是默认的

/usr/share/nginx/html/
路径,而不是在
myapp
目录下新添加的
/etc/nginx/sites-enabled/

nginx centos7
2个回答
1
投票

真正的问题是,操作系统发行版本和软件包版本,使得软件不同。

注意:这是 CentOS 7.3!

我用来安装nginx的方法是:

yum update
yum install epel-release
yum install nginx

然后,nginx 版本可能与 Ubuntu 上的包等其他版本略有不同。所以用法也不一样。

它的目录是:

/etc/nginx/nginx.conf
/etc/nginx/conf.d/
# Notice, there aren't these directories exist!
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/

所以用法不同,以下是必要的!

首先,注释掉

/etc/nginx/nginx.conf
中的默认设置:

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#   }

其次,在

/etc/nginx/conf.d/
下为应用程序创建新的配置:

# File Name: rails.conf
upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/home/deploy/myapp/tmp/sockets/unicorn.sock fail_timeout=0;
}

server {
    listen 192.168.0.1:80;
    server_name app1;

    # Application root, as defined previously
    root /home/deploy/myapp/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-FORWARDED_PROTO https;
        proxy_pass http://app;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
  }

如果

default.conf
下存在
/etc/nginx/conf.d/
,请将其删除。

第三,检查语法并重启nginx:

nginx -t
service nginx restart

运行时

/home/deploy/myapp/public
会访问指向
curl 192.168.0.1
的路径。


0
投票

您得到的错误是说 nginx 无法访问 /usr/share/nginx/html/ 的索引文件夹,这是在 app.conf 中热 tryfile @app 指令时发生的。原因是默认情况下 nginx 关闭 autoindex ;这意味着如果您请求 / 路径,则 try_file 将不允许它。 看: 自动索引

在您的情况下,您需要添加自动索引;服务器中 try_file 指令之前的指令。

© www.soinside.com 2019 - 2024. All rights reserved.