如何使用子域和 reverse_proxy 设置 nginx conf 并且不使用启用站点? [英] How do I set up an nginx conf with a subdomain and reverse_proxy and NO use of sites-enabled?

查看:71
本文介绍了如何使用子域和 reverse_proxy 设置 nginx conf 并且不使用启用站点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读到没有必要使用 启用站点,甚至看到它建议不要使用.

I've read that it's not necessary to use sites-enabled, and even seen it suggested not to use.

无论如何,它的优点不是问题的一部分(所以请考虑偏离主题的讨论).

In any case, its merit is not part of the question (so please consider that discussion off topic).

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

What I'm trying to do is set up an absolutely barebones basic nginx.conf file which does some super basic use case stuff: various forms of redirection.

根据我的理解,这个配置应该足够了:

By my understanding, this conf should be enough:

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 在这一点上做任何事情.

Unfortunately, my example doesn't work. The proxy bit works, but subdomains don't seem to. I don't honestly believe that server_name does anything at this point.

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

推荐答案

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

I tried your config at my sandbox VM. nginx refuses to start, and when I run nginx -t command (which is always a good idea after significant configuration change), it says:

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 成功启动,一切都按预期工作.

So I've added events {} line to the config. After that nginx successfuly starts and all is working as expected.

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

Another thing that I wouldn't skip is including mime.types file. So final minimal configuration would look as follows:

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;
  }
}

这篇关于如何使用子域和 reverse_proxy 设置 nginx conf 并且不使用启用站点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆