nginx強制使用https訪問(多站點多域名配置)


很多配置過https模板的人都知道,配置https 時 ,站在用戶的角度http 和https 的區別根本不清楚。有時候敲 http 時會出現 404 錯誤,而實際上我們是https.

有朋友找我配置一個多站點多域名,想着工作不是很忙,就花點時間給他配置下。

他的需求是這樣的:

  1.2個項目放在同一台服務器

  2.多站點,多域名(也就是說不同的域名訪問不同的項目,且其中一個項目使用https)

下面是我跟她配置的

server {
    listen       80;
    server_name  47.106.178.XXX  www.oyuanxing.com wx.oyuanxing.com;
    
    
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {

         rewrite ^(.*)$ https://$host$1 permanent;  
         if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
           }
        root   /usr/share/nginx/html/OGO/;
        index  index.html index.htm index.php;
    }

    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/OGO/;
    }

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
         root           /usr/share/nginx/html/OGO/;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/OGO/$fastcgi_script_name;
         include        fastcgi_params;
    }

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

server {
        listen       443 ssl;
		server_name   47.106.178.XXX www.oyuanxing.com wx.oyuanxing.com;
	   
		ssl on;
		ssl_certificate cert/1968881__oyuanxing.com.pem;
		ssl_certificate_key cert/1968881__oyuanxing.com.key;
		ssl_session_timeout 5m;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
		ssl_prefer_server_ciphers on;
        location / {
           if (!-e $request_filename) {
			rewrite  ^(.*)$  /index.php?s=/$1  last;
			break;
			}

            root   /usr/share/nginx/html/OGO/;
            index  index.html index.htm index.php;
        }
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html/OGO/;
        }

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

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /usr/share/nginx/html/OGO/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/OGO/$fastcgi_script_name;
            include        fastcgi_params;
        }
    }





server {
    listen       80;
    server_name   47.106.178.XXX  m.lhmhcc.com ;
    
    
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {


         if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
           }
        root   /usr/share/nginx/html/TOUTOU/;
        index  index.html index.htm index.php;
    }

    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/TOUTOU/;
    }

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
         root           /usr/share/nginx/html/TOUTOU/;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/TOUTOU/$fastcgi_script_name;
         include        fastcgi_params;
    }

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

  若是不加上面那句加粗的重寫規則,那么http 和 https 都能訪問。

此時多站點 多域名 就配好了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM