【问题标题】:How to correctly set virtual hosts on Ningx?如何在 Ningx 上正确设置虚拟主机?
【发布时间】:2014-06-19 05:33:41
【问题描述】:

尝试设置处理 2 个域的 Nginx 时遇到了一些问题。虽然我的两个域设置在静态 html 处理中正常工作,但尝试在 Nginx 后面推进并启动两个 python 应用程序。我尝试了一些不同的 wsgi 容器和不同的微框架,但问题是 Nginx 无法处理虚拟主机,而是在两个域地址上只为一个应用程序提供服务。

这里是 Nginx 配置文件:

user www-data;
worker_processes 8;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    server_names_hash_bucket_size 64;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_disable "msie6";

    server {
      listen 80;
      server_name www.domainA.com;
      root /var/www/domainA.com;

      location / {
         proxy_set_header    Host                $host;
         proxy_set_header    X-Real-IP           $remote_addr;
         proxy_set_header    X-Forwarded-For     $remote_addr;
         proxy_set_header    X-Originating-IP    $remote_addr;
         proxy_set_header    HTTP_REMOTE_ADDR    $remote_addr;
         proxy_set_header    REMOTE_ADDR         $remote_addr;
         proxy_set_header    CLIENT_IP           $remote_addr;
         proxy_pass http://127.0.0.2:7000;
      }
    }
    server {
      listen 80;
      server_name www.domainB.com;
      root /var/www/domainB.com;

      location / {
... ... blah blah...same story...except this proxy pass.....
         proxy_pass http://127.0.0.1:5000;
      }
    }
}

有什么帮助吗?

编辑:

刚刚尝试将空服务器块添加为第一个块,它返回 404。

【问题讨论】:

  • 可能你需要为 www.domainB.com 使用不同的监听端口
  • 您确定在配置更改后重新启动了 nginx 吗?您访问的是www.domainB.com,而不仅仅是domainB.com
  • 你能告诉我们你的相关python代码吗?

标签: python nginx flask cherrypy uwsgi


【解决方案1】:

这些是面向外部的网站吗?

如果你把完整的 ip 放在你的 listen 子句中,你应该可以开始正常工作了。

listen 512.548.595.485:80;

现在你有两个站点的服务器 ip 导致冲突。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    在虚拟主机共享 ip 和端口的场景中,nginx 通过将客户端发送的 Host 标头与每个服务器的 server_name 条目进行比较来选择正确的虚拟主机。如果您有 curl,请使用以下内容查看您为 Host 标头发送的确切内容:

    curl -s --trace-ascii - http://www.domainA.com | grep 'Host:'
    

    要使您的 server_name 更灵活,请使用 .example.com 表示法。这是 example.com 和 *.example.com 的简写。或者,只需根据需要添加尽可能多的 server_name 条目。

    接下来确认您的应用正在侦听正确的 ips 和端口。进入您的服务器并尝试:

    curl -I 'http://127.0.0.1:5000'
    curl -I 'http://127.0.0.2:7000'
    

    【讨论】:

    • 它返回相同的 - 0029:主机:www.domainA.com 或 0029:主机:www.domainB.com.....
    • @Alex 我添加了另一个测试。没关系,但您可以尝试将 127.0.0.2 更改为 127.0.0.1,因为您已经在不同的端口上监听了。
    • 我之前尝试过但没有成功,这就是为什么我以 0.2 结束:..请参阅我的问题中的编辑。
    【解决方案3】:

    最后我以这样的问题结束了。在测试条件下,我没有添加所有能让 Nginx 满意的味道。然后我找到了THIS LINK

    如果“Host”标头字段与服务器名称不匹配,NGINX 会将请求路由到该端口的默认服务器。默认服务器是 nginx.conf 文件中列出的第一个服务器。如果在服务器上下文中的 listen 指令中设置了 default_server 参数,这将被覆盖。下面给出一个例子。

    Nginx 文档和教程分散在少数几个网站上,因此找到的很少并不意味着您获得了所需的所有答案。

    【讨论】:

      【解决方案4】:

      我认为,这是您的解决方案。创建一个名为 virtualhost.sh 的 BASH 文件。复制并粘贴以下代码:

      #!/bin/bash
      
      domain=$1
      root="/data/$domain"
      block="/etc/nginx/sites-available/$domain"
      
      # Create the Document Root directory
      mkdir -p $root
      
      # Assign ownership to your regular user account
      chown -R $USER:$USER $root
      
      # Create the Nginx server block file:
      tee $block > /dev/null <<EOF 
      server {
          listen 80;
          listen [::]:80;
      
          root /data/$domain;
          index index.php index.html index.htm;
      
          server_name $domain www.$domain;
      
          location / {
              try_files $uri $uri/ =404;
          }
      
          location ~ \.php$ {
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              fastcgi_pass unix:/var/run/php5-fpm.sock;
              fastcgi_index  index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_buffer_size 128k;
              fastcgi_buffers 4 256k;
              fastcgi_busy_buffers_size 256k;
              include fastcgi_params;
          }
      
          location ~ /\.ht {
              access_log off;
              log_not_found off;
              deny all;
          }
      
          location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
              access_log        off;
              log_not_found     off;
              expires           30d;
          }
      
          location = /favicon.ico {
              log_not_found off;
              access_log off;
          }
      
          location = /robots.txt {
              allow all;
              log_not_found off;
              access_log off;
          }
      
      }
      
      EOF
      
      # Link to make it available
      ln -s $block /etc/nginx/sites-enabled/
      
      # Test configuration and reload if successful
      nginx -t && service nginx reload
      

      你需要调用这个 BASH 文件:

      virtualhost.sh www.yourdomain.com
      

      【讨论】:

        猜你喜欢
        • 2021-04-17
        • 2020-07-17
        • 2014-04-08
        • 2020-07-29
        • 2014-09-04
        • 2013-01-26
        • 2014-05-22
        • 2011-10-20
        • 2018-05-16
        相关资源
        最近更新 更多