https 需要購買域名ssl證書
注意事項:
1.要開啟HTTP/2協議支持,需要在nginx 1.10以上版本並且需要openssl庫的版本在1.0.2及以上編譯。
2.http2.0只支持開啟了https的網站。
openssl version 可以查看openssl版本
nginx -v 可以查看nginx版本
mysql -v 可以查看mysql版本
php -v 可以查看php版本
以下命令可以查看linux版本
uname -a;
more /etc/issue;
cat /proc/version;
開始:
這里我的證書是使用阿里雲提供的免費證書
1.下載證書
2.安裝證書
文件說明:
1. 證書文件1533488566332.pem,包含兩段內容,請不要刪除任何一段內容。
2. 如果是證書系統創建的CSR,還包含:證書私鑰文件1533488566332.key。
( 1 ) 在Nginx的安裝目錄下創建cert目錄,並且將下載的全部文件拷貝到cert目錄中。如果申請證書時是自己創建的CSR文件,請將對應的私鑰文件放到cert目錄下並且命名為1533488566332.key;
( 2 ) 打開 Nginx 安裝目錄下 conf 目錄中的 nginx.conf 文件,找到:
# HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # # #} #}
( 3 ) 將其修改為 (以下屬性中ssl開頭的屬性與證書配置有直接關系,其它屬性請結合自己的實際情況復制或調整) :
server { listen 443; server_name localhost; ssl on; root html; index index.html index.htm; ssl_certificate cert/1533488566332.pem; ssl_certificate_key cert/1533488566332.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; location / { root html; index index.html index.htm; } }
保存退出。
( 4 )重啟 Nginx。
( 5 ) 通過 https 方式訪問您的站點,測試站點證書的安裝配置。
問題:但是安裝后 執行 systemctl start nginx 報錯 the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
原來是nginx安裝時候沒有加載 ssl模塊 和 http2模塊
1)切換到nginx源碼包
cd /usr/local/src/nginx-1.11.3
2)查看ngixn原有的模塊
/usr/local/nginx/sbin/nginx -V
顯示 --prefix=/usr/local/nginx 需要后面加上需要的模塊--with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
--with-http_v2_module 是支持http2 的模塊 支持http2需要滿足
3)重新配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
4)重新編譯,不需要make install安裝。否則會覆蓋
make
5)備份原有已經安裝好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
6)將剛剛編譯好的nginx覆蓋掉原來的nginx(ngixn必須停止)
cp ./objs/nginx /usr/local/nginx/sbin/
這時,會提示是否覆蓋,請輸入yes,直接回車默認不覆蓋
7)啟動nginx,查看nginx模塊,發現已經添加
/usr/local/nginx/sbin/nginx -V
最后附上完整的ngixn配置:
user nginx nginx; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 8; gzip_vary on; gzip_types text/plain application/x-javascript text/css text/json application/xml text/javascript application/javascript; gzip_disable "MSIE"; server_tokens off; # sendfile on; #tcp_nopush on; #keepalive_timeout 0; # keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; root /data; #charset koi8-r; #access_log logs/host.access.log main; location / { root /data; index index.php index.html index.htm; } #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 /data; } # 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 /data; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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 80; server_name www.fibst.cn; return 301 https://$server_name$request_uri; } server{ #listen 80; listen 443 ssl http2; server_name www.fibst.cn; ssl on; ssl_certificate cert/www.fibst.cn.pem; ssl_certificate_key cert/www.fibst.cn.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; root /data/suxiaoying/; index index.html index.htm index.php; location ~ ^(.*)\/\.svn\/ { return 404; } location / { try_files $uri $uri/ /index.php$is_args$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; } } }