http - 我们什么时候需要在 nginx 配置文件中使用 http block ?

标签 http nginx

我正在阅读 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/

相关文章:

ios - Swift:访问通过post请求收到的JSON

C# 从 HttpWebResponse 解压并读取文件

python - "no python application found"uWSGI + nginx + Ubuntu 13

ruby-on-rails - 跨前端服务器共享缓存文件的可扩展方式

java - 检查 HTTP 请求中的图像有效性

.net - ASP.NET 中的 URL 解析

http - 当用户在浏览器中返回时,是否有一种跨浏览器的方法可以使上一页实际从服务器加载内容?

ajax - 使用 Nginx 作为代理,ajax 请求每隔一个请求得到 405

redirect - 内部重定向时 Nginx 重写或内部重定向循环

c# - 关于使用 Nginx 将 Web 请求代理到基于 .Net HttpListener 的 Json 服务的想法