nginx 支持https訪問


1,先確認nginx安裝時已編譯http_ssl模塊。

  就是執行nginx -V命令查看是否存在--with-http_ssl_module。如果沒有,則需要重新編譯nginx將該模塊加入。yum安裝的一般都編譯進去了。源碼編譯二進制包安裝的可能需要重新編譯,然后make,但是不需要make install。

2,查看是否已經安裝SSL。openssl version -a

3,生成SSL證書

#在nginx目錄下創建ssl文件夾
cd /etc/pki
mkdir nginx
cd nginx
#生成2048位的加密私鑰
openssl genrsa -out server.key 2048
生成證書簽名請求(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 []: ← 可以不輸入生成類型為X509的自簽名證書。

#有效期設置3650天,即有效期為10年
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

4、修改Nginx配置文件

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
     #原目錄名是html,是文件數據目錄,修改為http,可以和https的數據目錄做區分,也可以設置成一樣的。  root /usr/share/nginx/http; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name _; root /usr/share/nginx/https; ssl_certificate "/etc/pki/nginx/server.crt"; ssl_certificate_key "/etc/pki/nginx/server.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

 

詳細可參考以下博客:  

https://www.cnblogs.com/bcodepod/p/15325522.html

https://cloud.tencent.com/developer/article/1151073


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM