鍍金池/ 問答/PHP  Android  Linux/ nginx實(shí)現(xiàn)同一臺(tái)服務(wù)器配置兩個(gè)網(wǎng)站

nginx實(shí)現(xiàn)同一臺(tái)服務(wù)器配置兩個(gè)網(wǎng)站

我在一臺(tái)虛擬機(jī)上(同一個(gè)IP)搭建了LNMP環(huán)境,然后部署兩個(gè)網(wǎng)站(兩個(gè)域名),分別時(shí)www.iwwenbo.com,www.weiyanzixun.com,域名解析正常,同時(shí)開啟了https證書。現(xiàn)在的問題是我訪問www.iwwenbo.com這個(gè)域名時(shí),經(jīng)常會(huì)跳轉(zhuǎn)到www.weiyanzixun.com這個(gè)域名上,請問我的配置問題出在哪里?
環(huán)境:

nginx version: nginx/1.12.1

兩個(gè)域名的nginx配置文件分別如下:

配置1:.iwwenbo.https.conf

server{
    listen 80;
    server_name iwwenbo.com www.iwwenbo.com;
    add_header Strict-Transport-Security max-age=15768000;
    return 301 https://$server_name$request_uri;
}

server {
   listen 443 ssl http2;
   server_name iwwenbo.com www.iwwenbo.com;
   index  index.php index.html index.htm;
   root   /usr/share/nginx/html;
   add_header X-Frame-Options DENY;
   add_header X-Content-Type-Options nosniff;
 
   ssl_certificate /etc/letsencrypt/live/iwwenbo.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/iwwenbo.com/privkey.pem;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
   ssl_prefer_server_ciphers on;
   ssl_session_cache shared:SSL:10m;
   ssl_session_timeout 60m;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }

}

配置2:weiyan.https.conf

server{
    listen 80;
    server_name weiyanzixun.com www.weiyanzixun.com;
    #告訴瀏覽器有效期內(nèi)只準(zhǔn)用 https 訪問
    add_header Strict-Transport-Security max-age=15768000;
    #永久重定向到 https 站點(diǎn)
    return 301 https://$server_name$request_uri;
}

server {
   listen 443 ssl http2;
   server_name weiyanzixun.com www.weiyanzixun.com;
   index  index.php index.html index.htm;
   root   /usr/share/nginx/weiyanzixun;
   add_header X-Frame-Options DENY;
   add_header X-Content-Type-Options nosniff;
 
   ssl_certificate /etc/letsencrypt/live/weiyanzixun.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/weiyanzixun.com/privkey.pem;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
   ssl_prefer_server_ciphers on;
   ssl_session_cache shared:SSL:10m;
   ssl_session_timeout 60m;


    location / {
        try_files $uri $uri/ /index.php?$args;  #修改內(nèi)容
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #修改此處內(nèi)容支持php
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

請哪位大神告訴我問題出在哪里,還有就是這個(gè)配置有那些地方可以進(jìn)行優(yōu)化的?

回答
編輯回答
萌二代

一個(gè)443就OK了

2018年6月26日 15:07
編輯回答
抱緊我

兩個(gè)都是fastcgi_pass 127.0.0.1:9000;會(huì)有問題么?

2017年4月13日 21:08
編輯回答
乞許

你用 /你的nginx安裝路徑/bin/nginx -t 檢查一下你的配置,報(bào)錯(cuò)嗎?

2018年2月13日 02:55
編輯回答
入她眼

您location并沒有指定root(文件目錄),以下是我的個(gè)人服務(wù)器上的配置,供您參考:

server {
    listen 80;
    server_name  *.sosout.com;
    
    if ($host ~* "^(.*?)\.sosout\.com$") {
        set $domain $1;
    }

    location / {
        if ($domain ~* "blog") {
            root /mnt/html/blog;
        }
        if ($domain ~* "strap") {
            root /mnt/html/vueStrap;
        }
        if ($domain ~* "admin") {
            root /mnt/html/admin;
        }
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto  $scheme;
    }
    access_log  /mnt/logs/nginx/access.log  main;
}

不同的域名指向了不同的目錄。

2018年6月20日 09:53
編輯回答
何蘇葉

這個(gè)配置沒有問題,建議檢查是否有瀏覽器緩存。

另外建議用curl或者瀏覽器的開發(fā)者工具禁用緩存來測試

http的配置可優(yōu)化:

server {
    listen 80;
    server_name www.iwwenbo.com iwwenbo.com;
    return 301 https://$server_name$request_uri;
}

樓主的https裸域是301跳轉(zhuǎn)到www的,所以http的配置建議把www放在裸域的前面,減少一次跳轉(zhuǎn)
$server_name只能獲取到server_name指令的第一個(gè)域名

去掉add_header Strict-Transport-Security max-age=15768000;,所有瀏覽器在http協(xié)議下都是忽略這個(gè)頭部的,這個(gè)頭部只有在https協(xié)議下才有效
如果需要添加只能添加到https里面

2018年2月22日 10:13