nginx强制使用https访问(多站点多域名配置)

时间:2021-04-27 15:18:53

很多配置过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 都能访问。

此时多站点 多域名 就配好了。