給本身網站配置 https,http2 ,gzip壓縮

https 須要購買域名ssl證書javascript

注意事項:php

1.要開啓HTTP/2協議支持,須要在nginx 1.10以上版本而且須要openssl庫的版本在1.0.2及以上編譯。css

2.http2.0只支持開啓了https的網站。html

 openssl version 能夠查看openssl版本java

 nginx -v 能夠查看nginx版本node

 mysql -v 能夠查看mysql版本mysql

 php -v 能夠查看php版本linux

 如下命令能夠查看linux版本nginx

 

uname -a;sql

 

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


}
相關文章
相關標籤/搜索