关于nginx服务器块对应的二级域名

关于nginx服务器块对应的二级域名

有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. 是否必须需要两个服务器块?如果有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 将保护您免受格式错误的域名的侵害,这些域名可用于仅通过拒绝无效域名来设置错误的根位置,因此安全性在这里应该不是什么大问题。

相关内容