nginx實現https網站設置(SSL證書生成配置)

1、HTTPS簡介
1.https簡介
HTTPS實際上是有兩部分組成:HTTP + SSL / TLS,也就是在HTTP上又加了一層處理加密信息的模塊。服務端和客戶端的信息傳輸都會經過TLS進行加密,因此傳輸的數據都是加密後的數據
2.https協議原理
首先,客戶端與服務器創建鏈接,各自生成私鑰和公鑰,是不一樣的。服務器返給客戶端一個公鑰,而後客戶端拿着這個公鑰把要搜索的東西加密,稱之爲密文,並連並本身的公鑰一塊兒返回給服務器,服務器拿着本身的私鑰解密密文,而後把響應到的數據用客戶端的公鑰加密,返回給客戶端,客戶端拿着本身的私鑰解密密文,把數據呈現出來
2、證書和私鑰的生成
注意:通常生成的目錄,應該放在nginx/conf/ssl目錄
1.建立服務器證書密鑰文件 server.key:
openssl genrsa -des3 -out server.key 1024
輸入密碼,確認密碼,本身隨便定義,可是要記住,後面會用到。
2.建立服務器證書的申請文件 server.csr
openssl req -new -key server.key -out server.csr
輸出內容爲:
Enter pass phrase for root.key: ← 輸入前面建立的密碼 
Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 能夠不輸入 
Common Name (eg, YOUR name) []: ← 此時不輸入 
Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填
Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 能夠不輸入 
An optional company name []: ← 能夠不輸入
4.備份一份服務器密鑰文件
cp server.key server.key.org
5.去除文件口令
openssl rsa -in server.key.org -out server.key
6.生成證書文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
    3、配置文件

1.下面爲配置文件 /usr/local/nginx/conf/vhost/daj.confphp

server{
#比起默認的80 使用了443 默認 是ssl方式  多出default以後的ssl
        listen 443 default ssl;
#default 可省略
#開啓  若是把ssl on;這行去掉,ssl寫在443端口後面。這樣http和https的連接均可以用
        ssl on;
#證書(公鑰.發送到客戶端的)
        ssl_certificate ssl/server.crt;
#私鑰,
        ssl_certificate_key ssl/server.key;
#下面是綁定域名
        server_name www.daj.com;
        location / {
#禁止跳轉
        proxy_redirect off;
#代理淘寶
proxy_pass https://www.tao.com/;  
        }        
}html

 
4、開啓nginx的ssl模塊
1.the "ssl" parameter requires ngx_http_ssl_module  in /usr/local/nginx/conf/nginx.conf:37
緣由是nginx缺乏http_ssl_module模塊,編譯安裝時帶上--with-http_ssl_module配置就能夠了
2.若是已經安裝過nginx,想要添加模塊看下面
1)切換到nginx源碼包
cd /usr/local/src/nginx-1.11.3
2)查看ngixn原有的模塊
/usr/local/nginx/sbin/nginx -V
3)從新配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_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 

 

nginx 配置:nginx

nginx.conf 配置:git

user  nobody;
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;

    client_max_body_size 10m;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {

    listen       80;
    server_name  172.16.21.123;

    location / {
        rewrite ^(.*)$ https://172.16.21.123:433$1 permanent;
    
    }
#    rewrite ^(.*)$ https://$host$1 permanent;
    }

    server { # listen 80; listen 433; ssl on; ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.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; server_name 172.16.21.123;
#    rewrite ^(.*)$ https://172.16.21.123$1 permanent;    


        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/projects/pages/dist;
            index  index.html index.htm;
        }
        
    location ^~/api/ {
                proxy_pass         http://172.16.21.161:8899/;
        proxy_redirect     off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

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

        # 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           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

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