微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
    
    
    #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;
	   
		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 都能访问。

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐