Nginx配置https教程

概述

公司的做的平臺項目越來越多,急切的需要把文件服務獨立出來做成公共服務,供各個項目使用,所以就需要配置文件服務器了。

openssl生成SSL證書的流程

下載openssl https://www.openssl.org/source/

  1. 生成key 和scr證書 ,在openssl根目錄執行:
openssl req -new -newkey rsa:2048 -sha256 -nodes -out server.csr -keyout server.key -subj "/C=CN/ST=ShenZhen/L=ShenZhen/O=Example Inc./OU=Web Security/CN=example.com"
  1. 用key和scr生成證書(.crt) ,在openssl根目錄執行:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

2.nginx安裝openssl模塊(nginx已經安裝沒有安裝openssl模塊)

  1. 查看nginx原有的模塊/usr/local/nginx/sbin/nginx -V :會顯示在–prefix=/usr/local/nginx --with-http_stub_status_module
  2. 進入nginx源碼包目錄,運行 :./configure --prefix=/usr/local/nginx --with-http_stub_status_module –with-http_ssl_module
  3. 配置完成後,運行命令make命令: make
  4. 替換已安裝好的nginx包: cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  5. 將nginx源碼包中生成objs裏的nginx拷貝到sbin: cp ./objs/nginx /usr/local/nginx/sbin/
  6. 然後啓動nginx,仍可以通過命令查看是否已經加入成功: /usr/local/nginx/sbin/nginx -V
  7. 此時應該顯示爲即配置成功: configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module –with-http_ssl_module

3.nginx配置文件

將openssl生成的證書放到指定的位置備用,添加一個server

server
{
listen 4443 ssl;
server_name localhost;
#ssl off;
#root /usr/share/nginx/html;
#index index.html index.htm;
charset utf-8;

#ssl_certificate /etc/nginx/ssl/nginx.crt;
#ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_certificate           /usr/local/nginx/ca/server/server.crt;
ssl_certificate_key       /usr/local/nginx/ca/server/server.key;
# ssl_client_certificate    /usr/local/nginx/ca/private/ca.crt;
# ssl_verify_client on;
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 ~ .*\.(gif|jpg|jpeg|png)$ {
   expires 24h;
     root /home/share/test/;#指定圖片存放路徑
     proxy_store on;
     proxy_store_access user:rw group:rw all:rw;
     proxy_temp_path     /home/share/test;#圖片訪問路徑
     proxy_redirect     off;
     proxy_set_header    Host 127.0.0.1;
     client_max_body_size  10m;
     client_body_buffer_size 1280k;
     proxy_connect_timeout  900;
     proxy_send_timeout   900;
     proxy_read_timeout   900;
     proxy_buffer_size    40k;
     proxy_buffers      40 320k;
     proxy_busy_buffers_size 640k;
     proxy_temp_file_write_size 640k;
     if ( !-e $request_filename)
     {
    #    proxy_pass http://127.0.0.1:82;#默認80端口
     }
}

}

4. 問題與注意

  1. 如果在sbin下面執行 ./nginx -V 和 nginx -V顯示的結果不一致 :那就把sbin下面的nginx拷貝到/usr/sbin/下面

  2. 錯誤提示./configure: error: SSL modules require the OpenSSL library
    解決方法:
    Centos需要安裝openssl-devel
    Ubuntu則需要安裝:sudo apt-get install libssl-dev

  3. https獲取文件服務器路徑403 :文件服務目錄和文件權限不夠,如果是web項目生成的文件目錄權限不夠,可能是tomcat版本引起的

    檢查tomcat
    打開bin/catalina.sh文件,檢查大概在263行左右。
    將0027改改爲0022,重啓tomcat,上傳訪問的問題解決。

if [ -z "$UMASK" ]; then
  UMASK="0027"
fi
if [ -z "$UMASK" ]; then
  UMASK="0022"
fi
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章