Nginx配置同一個域名http與https兩種方式均可訪問,證書是阿里雲上免費申請的php
server { listen 80; listen 443 ssl; ssl on; server_name 域名; index index.html index.htm index.php default.html default.htm default.php; ssl_certificate /usr/local/nginx/cert/21402058063066221.pem; //下載申請後阿里ssh提供的pem ssl_certificate_key /usr/local/nginx/cert/21402058063066221.key;//下載申請後阿里ssh提供的key ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; root /home/wwwroot/網站目錄; include laravel.conf; //好吧,這裏是laravel配置,不必定合適您哈,請或略 #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/airclass.mime.org.cn.log; }
關鍵在於上面的listen 80;
listen 443 ssl; 開啓80端口css
固然,這樣玩就沒有啥意義了,既然是https,就徹底不必http傳輸數據啦.咱們必須把全部http請求轉發到https,html
把http重定向到https使用了nginx的重定向命令。那麼應該如何寫重定向?以前老版本的nginx可能使用瞭如下相似的格式。
也就是再添加一個虛擬機server,80端口一個nginx
server { listen 80; server_name www.domain.com; rewrite ^/(.*) https://$server_name$1 permanent; #跳轉到Https }
重寫依舊不一樣版本可能以下laravel
rewrite ^/(.*)$ https://domain.com/$1 permanent;
或者web
rewrite ^ https://domain.com$request_uri? permanent;
如今nginx新版本已經換了種寫法,上面這些已經再也不推薦。如今網上可能還有不少文章寫的是第一種。session
下面是nginx http頁面重定向到https頁面最新支持的寫法:dom
server { listen 80; server_name domain.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name domain.com; }
可是個人nginx/1.10.0好像跑不起來,也許不支持這種寫法吧...
下面是基於http轉https的完整配置:ssh
server { #listen 80; listen 443; ssl on; server_name domain.com; //你的域名 index index.html index.htm index.php default.html default.htm default.php; ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem; ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; root /home/wwwroot/web/public;//項目根目錄 include laravel.conf; #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } } server { listen 80; server_name domain.com; rewrite ^/(.*) https://$server_name$request_uri? permanent; }