在CentOS 7如何在CentOS 7上403 Forbidden時正確設置Nginx?
/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:下/etc/nginx/sites-available/
名
# ...
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 {
}
}
# ...
創建一個新的文件:
yum install nginx
在/etc/nginx/nginx.conf
d 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
然後嘗試訪問網址:
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禁止。
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/
目錄?
使用不同的端口,然後再試一次 – mononoke
默認的conf仍然在sites-enabled目錄中嗎?您可能沒有禁用默認網站並啓用您的myapp網站。 – Chase
@Chase否,'sites-available'和'sites-enabled'文件夾都只有一個名爲'myapp'的文件。它沒有'default'。 –