我正在阅读 nginx 初学者教程,在 Serving Static Content 部分他们有
http {
server {
}
}
但是当我添加一个 http block 时我得到了错误
[emerg] "http" directive is not allowed here …
当我删除 http block 并将 conf 文件更改为此时,它工作正常:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/example.com/html;
index index.html index.htm;
# make site accessible from http://localhost/
server_name localhost
location / {
try_files $uri $uri/ /index.html;
}
我怀疑我遗漏了一些简单的东西,但为什么他们使用 http 来提供静态文件?
最佳答案
你做的很好。我猜你正在编辑/etc/nginx/sites-enabled/default (或/etc/nginx/sites-available/default 的链接文件。
这是标准的 nginx 设置。它配置有/etc/nginx/nginx.conf,其中包含 http {} 语句。这又包含一个“include/etc/nginx/sites-enabled/*”行,以包含上面带有 server{} 子句的文件。
请注意,如果您使用的是创建备份文件的编辑器,则必须修改包含语句以排除备份文件,否则您会得到一些“有趣”的错误!我的线路是
include /etc/nginx/sites-enabled/*[a-zA-Z]
不会获取以波浪号结尾的备份文件。 YMMV。
关于http - 我们什么时候需要在 nginx 配置文件中使用 http block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639568/