【问题标题】:IPv4 Public IP and Public DNS (IPv4) serving different content服务不同内容的 IPv4 公共 IP 和公共 DNS (IPv4)
【发布时间】:2018-11-08 02:10:50
【问题描述】:

我有一个在 AWS EC2 实例上运行的 Django-nginx-gunicorn 应用程序。我已经配置好nginx,可以获取公网IP上的所有内容了。但是 AWS 提供的外部 DNS 主机名并没有发生同样的情况(例如:ec2-203-0-113-25.compute-1.amazonaws.com)。我的仍在渲染 nginx 默认页面。公共 IP x.x.x.x 正在为 Django App 内容提供服务。

这里是server nginx.conf 的一部分:

server {
        listen 80;
        server_name X.X.X.X; # Has the Public IP address here
        access_log /var/log/nginx-access.log;
        error_log /var/log/nginx-error.log;
        root /home/ubuntu/project/ToDo-application/;
        location /static {
                root /home/ubuntu/ToDo-application/frontend;
        }

        location / {
                include /etc/nginx/mime.types;
                default_type application/octet-stream;
                proxy_pass http://127.0.0.1:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

如何让默认 DNS 提供与公共 IP 相同的内容?

【问题讨论】:

    标签: amazon-web-services nginx amazon-ec2


    【解决方案1】:

    从您的配置中删除 server_name:

        server_name X.X.X.X
    

    变化:

        listen 80;
    

    收件人:

        listen *:80;
    

    检查 /etc/nginx/sites-available 和/或 /etc/nginx/sites-enabled,应该有一个默认配置文件(默认)。我的猜测是,它保存了您的 DNS 指向的默认配置,并在 /var/www/html 下呈现 html。

    注释掉那个文件中的内容...然后,重启nginx:

        service nginx restart
    

    这里发生的情况是,一个配置告诉 nginx 绑定到端口 80,但查找您的公共 IP 的标头,另一个配置(默认配置)可能告诉它使用 DNS 作为服务器名称。通过绑定到 *:80,没有服务器名称,您告诉 nginx 在所有 IP 地址上提供您的配置,而不考虑服务器名称标头。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      server {
              listen 80 default_server;
              server_name X.X.X.X; # Has the Public IP address here
              access_log /var/log/nginx-access.log;
              error_log /var/log/nginx-error.log;
              root /home/ubuntu/project/ToDo-application/;
              location /static {
                      root /home/ubuntu/ToDo-application/frontend;
              }
      
              location / {
                      include /etc/nginx/mime.types;
                      default_type application/octet-stream;
                      proxy_pass http://127.0.0.1:8000;
                      proxy_set_header Host $host;
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }
      }
      

      根据官方Nginx documentation,使用default_server可以告诉Nginx特定的server定义应该是给定端口的默认监听器。

      还值得注意的是,您可以在一个 server_name 行中包含多个域名、IP 和正则表达式模式,以表明服务器应该阻止旨在处理列出的选项。

      【讨论】:

      • 我尝试了你的方法并重新启动了nginx,但如果失败了。所以我跑了nginx -t 并收到这条消息nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/nginx.conf:66 nginx: configuration file /etc/nginx/nginx.conf test failed 第66 行是listen 80 default_server;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 2017-02-10
      • 1970-01-01
      • 2015-09-06
      • 2015-12-01
      • 2022-01-25
      • 2021-02-04
      相关资源
      最近更新 更多