打印

[Nginx] 一个网站多个域名怎么配置?

一个网站多个域名怎么配置?

网站原来的域名 www.example1.com:配置如下
复制内容到剪贴板
代码:
server{
listen 80;
server_name www.example1.com example1.com;
index index.html index.htm  index.php;
root /alidata1/web/www.example1.com;
access_log off;
error_page 404 /404.html;
#error_page 403 /403.html;
if ($scheme = http ) {
     return 301 https://$host$request_uri;
    }
}

server {
  listen 443 ssl http2;
  server_name example1.com www.example1.com
  access_log off;
  index index.html index.php;
  root /alidata1/web/www.example1.com;

if ($host = 'example1.com') {  return 301 https://www.example1.com$request_uri;  }

# 阻止扫描器
  if ($bad_bot) {
        return 403;
    }

  if ($request_method !~ ^(GET|HEAD|POST)$) {
          return 444;
        }

   http2_push_preload on;
  
  error_page 404 /404.html;
  #error_page 502 /502.html;

  ssl on;
  ssl_stapling on;
  ssl_stapling_verify on;
  ssl_trusted_certificate  /etc/letsencrypt/acme/fullchain.cer;
  ssl_certificate   /etc/letsencrypt/acme/fullchain.cer;
  ssl_certificate_key /etc/letsencrypt/acme/*.example1.com.key;
  ssl_session_timeout 15m;
  ssl_session_cache shared:SSL:50m;
  #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_protocols  TLSv1.1 TLSv1.2 TLSv1.3;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
  ssl_prefer_server_ciphers on;


  location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

     # resolve cros
  location ~* \.(eot|ttf|woff|woff2|svg|otf|css|js) {
    add_header Access-Control-Allow-Origin *;

  }


  # cache .html files
  location ~.*\.(html|xml|json)$ {
    expires 30d;
  }
  # cache images files
  location ~ .*\.(webm|webp|gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico|html)$ {
    expires 360d;
    access_log off;
    log_not_found off;
  }
  # cache js,css files
  location ~ .*\.(js|css)?$ {
    expires 360d;
    access_log off;
    log_not_found off;
  }
}
现在想增加一个域名访问 example2.com 改成如下配置:
复制内容到剪贴板
代码:
server{
listen 80;
server_name www.example1.com example1.com www.example2.com example2.com;
index index.html index.htm index.php;
root /alidata1/web/www.example1.com;
access_log off;
error_page 404 /404.html;
#error_page 403 /403.html;
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
}

server {
listen 443 ssl http2;
server_name example1.com www.example1.com www.example2.com example2.com;
access_log off;
index index.html index.php;
root /alidata1/web/www.example1.com;

if ($host = 'example1.com') { return 301 https://www.example1.com$request_uri; }if ($host = 'example2.com') { return 301 https://www.example2.com$request_uri; }

# 阻止扫描器
if ($bad_bot) {
return 403;
}

if ($request_method !~ ^(GET|HEAD|POST)$) {
return 444;
}

http2_push_preload on;

error_page 404 /404.html;
#error_page 502 /502.html;

ssl on;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/acme/fullchain.cer;
ssl_certificate /etc/letsencrypt/acme/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/acme/*.example1.com.key;
ssl_session_timeout 15m;
ssl_session_cache shared:SSL:50m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
ssl_prefer_server_ciphers on;


location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}

# resolve cros
location ~* \.(eot|ttf|woff|woff2|svg|otf|css|js) {
add_header Access-Control-Allow-Origin *;

}


# cache .html files
location ~.*\.(html|xml|json)$ {
expires 30d;
}
# cache images files
location ~ .*\.(webm|webp|gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico|html)$ {
expires 360d;
access_log off;
log_not_found off;
}
# cache js,css files
location ~ .*\.(js|css)?$ {
expires 360d;
access_log off;
log_not_found off;
}
}
*.example1.com 和*.example2.com的证书公用。
配置之后可以访问www.example2.com 但是页面加载到一定时间之后,URL又自己跳到www.example1.com
所以,我的配置应该怎么改一下?

TOP