nginx ssl配置 實現http自動跳轉到https


#性能配置 一般配置cpu的核數
worker_processes 64;
events {
worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;

       #設定虛擬主機配置1
  server {

    #監聽5148端口,你的可能是80或者8080
    listen 5148;
    #如果有防火牆要映射內網ip到一個外網ip,這里寫內網即服務器的ip
    server_name 192.168.0.172;

    location / {
    root html;
    index index.html index.htm;
    }
    #rewrite ^(.*) https://$host$1 permanent;

    #所有5148的請求都 跳轉到https 處理
    return 301 https://$host$request_uri; 

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }

  }

  #設定虛擬主機配置2  
  server {

    #偵聽443端口,這個是ssl訪問端口
    listen 443 ssl;

    #定義使用 訪問域名 #如果有防火牆要映射內網ip到一個外網ip,這里寫內網即服務器的ip
    server_name 192.168.0.172;

    #定義服務器的默認網站根目錄位置
    root /usr/share/nginx/html;

    ssl_certificate /opt/my_key_store.crt; #證書文件
    ssl_certificate_key /opt/my_store.key; #證書文件
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;0


    location ^~ /oms/ {
    proxy_redirect http:// https://; #這行解決服務器內部 http沒跳轉到https的問題
    proxy_set_header Host $host;
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://192.168.0.172:7001/oms/;
    }

    #默認請求
    location / {
    root html;
    #定義首頁索引文件的名稱
    index index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }

    #靜態文件,nginx自己處理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
    #過期30天,靜態文件不怎么更新,過期可以設大一點,
    #如果頻繁更新,則可以設置得小一點。
    expires 30d;
    }

  }

}

 

#以上一般修改紅色部分就夠用了


免責聲明!

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



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