在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/
的{{1}}下创建了一个新文件:
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/
重新启动nginx:
cd /etc/nginx/sites-enabled/
ln -s ../sites-available/myapp
然后尝试访问url:
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
文件,因此获得了403 Forbidden。
Nginx错误日志/usr/share/nginx/html
:
/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
?
答案 0 :(得分:0)
你得到的错误是说nginx无法访问/ usr / share / nginx / html /的索引文件夹,当它在app.conf中运行tryfile @app指令时会发生这种情况。是默认情况下nginx有autoindex off;这意味着如果您请求/ path,它将不允许在try_file上。 看到: autoindex
在您的情况下,您需要添加自动索引;在try_file指令之前的服务器中的指令。
答案 1 :(得分:0)
真正的问题是,操作系统分发版本和软件包版本使软件不同。
注意:它是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
的路径。