nginx常用配置系列-HTTPS配置


接上篇,nginx配置系列

HTTPS現在已經很流行,特別是AppStore上線的應用要求使用HTTPS進行通信,出於安全考慮也應該使用HTTPS,HTTPS配置需要准備證書文件,現在也有很多免費證書可以申請,比如阿里雲

證書相關有兩個文件,一個key文件server.key,一個證書文件server.crt(證書文件的格式有很多(pem,p12,crt等)一般使用pem或crt,nginx都支持)

直接看配置代碼(example.com.conf文件)

server {
    # HTTPS 默認443端口
    listen 443 ssl;
    # 證書文件配置,指定證書的路徑,除了證書路徑其他配置都默認
    ssl_certificate     /usr/local/nginx/ssl/server.crt;
    ssl_certificate_key /usr/local/nginx/ssl/server.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5:!DH;
    
    # host
    server_name example.com www.example.com;
    
    #設置長連接
    keepalive_timeout 70;    
    #減少點擊劫持
    add_header X-Frame-Options DENY;
    #禁止服務器自動解析資源類型
    add_header X-Content-Type-Options nosniff;
    #防XSS攻擊
    add_header X-Xss-Protection 1;
    
    # 默認index
    index index.html index.htm index.php default.html default.htm default.php;
    # 代碼的根目錄
    root  /home/wwwroot/example;
    # 訪問日志
    access_log  /home/wwwlogs/example.com.log  main;
    
    # 文件的規則(詳見http://seanlook.com/2015/05/17/nginx-location-rewrite/index.html)
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }
    location ~ .*\.(js|css)?$ {
        expires      12h;
    }
}

# 全站使用HTTPS,讓通過HTTP訪問的用戶301跳轉到HTTPS
server {
    listen      80;
    server_name example.com www.example.com;
    #使用return的效率會更高
    return 301 https://$server_name$request_uri;
}

這個配置最需要關注的就是證書文件的配置,再就是HTTPS的端口默認是443,如果全站使用HTTPS的話,需要讓80端口的HTTP請求跳轉到HTTPS請求即可

============= nginx HTTPS 配置完畢 ==============

 

文中有不足指出請直接指出,一起學習討論,QQ:1485619676


免責聲明!

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



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