nginxサーバーブロックに対応する2次ドメイン名について

nginxサーバーブロックに対応する2次ドメイン名について

2 つの 2 次ドメイン名があり、次の 2 つの対応するフォルダがあります。

domain name    folder
111.aa.com     /var/www/111.aa.com
222.aa.com     /var/www/222.aa.com

にはnginx.conf、次のように2つのサーバーブロックがあります。

#111.aa.com

server {
    listen       80;
    server_name  111.aa.com;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/111.aa.com;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}


    location ~ \.php$ {
        root           /var/www/111.aa.com;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

#222.aa.com

server {
    listen       80;
    server_name  222.aa.com;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/222.aa.com;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}


    location ~ \.php$ {
        root           /var/www/222.aa.com;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

質問:

  1. 2つのサーバーブロックが必要ですか?セカンダリドメイン名が100個あり、サーバーブロックを100個書きたい場合は、サーバーブロック1個に書き込むことができますか?サーバーブロックに書き込むには?どんな例がありますか?

  2. 1つのサーバーブロックを書き込むことと、それぞれ100個のサーバーブロックを書き込むことのどちらが優れていますか?

ベストアンサー1

次の操作を実行できます。

server {
  server_name *.aa.com;
  root /var/www/$http_host;
  location / {
    try_files $uri $uri/ 404=@redirect;
  }

  location @redirect {
    return 301 http://aa.com;
  }
}

ドメイン名には「..」または「/」を含めることはできないため、Nginxは無効なドメイン名を拒否することによって無効なルートの場所を設定するために使用できる無効なドメイン名からユーザーを保護するため、セキュリティはここでは何もありません。大きな問題です。

おすすめ記事