如何使用子域和 reverse_proxy 设置 nginx conf,并且不使用启用站点?

How do I set up an nginx conf with a subdomain and reverse_proxy and NO use of sites-enabled?

我读到没有必要使用 , and even seen it suggested not to use

无论如何,它的优点不是问题的一部分(因此请考虑该讨论的主题)。

我想做的是建立一个绝对准系统的基本 nginx.conf 文件,它执行一些超级基本的用例:各种形式的重定向。

据我了解,这个 conf 应该足够了:

http {
  # default server
  server {
    root /var/www/html/production-site;

    # reverse proxy for external blog, makes example.com/blog display the blog.  helps with SEO.
    location /blog/ {
      proxy_pass https://example.some-external-blog.com/;
    }
  }

  # dev server
  server {
    server_name dev.example.com;
    root /var/www/html/dev-site;
  }
}

不幸的是,我的示例不起作用。代理位有效,但子域似乎无效。老实说,我不相信 server_name 在这一点上有任何作用。

那么,如何编写一个简单的(没有额外的)nginx.conf 文件来举例说明这些超级琐碎的功能(子域和反向代理)?

我在我的沙盒 VM 上尝试了您的配置。 nginx 拒绝启动,当我 运行 nginx -t 命令(在重大配置更改后这总是一个好主意)时,它说:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] no "events" section in configuration
nginx: configuration file /etc/nginx/nginx.conf test failed

所以我在配置中添加了 events {} 行。之后 nginx 成功启动,一切都按预期工作。

我不会跳过的另一件事是包括 mime.types 文件。所以最终的最小配置如下所示:

events {}

http {
  include mime.types;

  # default server
  server {
    root /var/www/html/production-site;

    # reverse proxy for external blog, makes example.com/blog display the blog.  helps with SEO.
    location /blog/ {
      proxy_pass https://example.some-external-blog.com/;
    }
  }

  # dev server
  server {
    server_name dev.example.com;
    root /var/www/html/dev-site;
  }
}