1、下載nginx源碼包並解壓
可在http://nginx.org/en/download.html下載.tar.gz的源碼包,如(nginx-1.4.7.tar.gz)
下載后通過tar -xvzf 進行解壓,解壓后的nginx目錄結構如下:
2、為nginx設置安裝目錄和啟用的模塊
切換到解壓后的nginx目錄中執行:
./configure --prefix=/opt/demo/nginx --add-module=/home/fastdfs-nginx-module/src --with-http_stub_status_module --with-http_ssl_module
參數說明:
--prefix 用於指定nginx編譯后的安裝目錄
--add-module 為添加的第三方模塊,此次添加了fdfs的nginx模塊
--with..._module 表示啟用的nginx模塊,如此處啟用了http_ssl_module模塊
可能出現的錯誤:
出現:./configure: error: the HTTP rewrite module requires the PCRE library.
解決方法:yum -y install pcre-devel
出現:SSL modules require the OpenSSL library
解決方法:yum install openssl-devel
3、編譯
執行make 進行編譯,如果編譯成功的話會在第一步中objs中出現一個nginx文件
特別注意:
在已安裝的nginx上進行添加模塊的話執行到這里就行了,把objs中的nginx替換掉之前的安裝的nginx/sbin/中的nginx文件,然后重啟nginx就行了,如果執行下一步的install,會導致之前安裝的nginx被覆蓋,比如之前配置好的nginx.conf文件)
4、安裝
執行make install 進行安裝,安裝后--prefix 中指定的安裝目錄下回出現如下目錄結構
5、啟動nginx
切入到第四步中的sbin目錄或是創建一個nginx軟鏈接
ln -s /opt/demo/nginx/sbin/nginx /usr/bin/nginx
完成后執行:
nginx start(如需開機自啟,可在/etc/rc.d/rc.local 文件中添此命令)
如出現:nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
則需通過nginx –c ../conf/nginx.conf 命令指定nginx的配置
nginx的一些常用管理命令
重啟:nginx -s reload
停止:nginx -s stop或者是通過kill nginx進程號
查看版本:nginx –V
關於nginx.conf配置文件
在安裝完nginx后會在conf目錄中產生一個nginx.conf的配置文件
里面有些默認配置,可根據自己的需求進行更改
示例:
#user nobody; worker_processes 1; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; proxy_connect_timeout 600s; proxy_read_timeout 600s; proxy_send_timeout 600s; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_ignore_client_abort on; client_max_body_size 200m; #此參數在使用fdfs上傳可控制上傳文件的大小 #日志的輸出格式,如需打印請求的body參數信息,可在$body_bytes_sent后添加 $request_body 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; #設置日志輸出的位置 access_log on; #是否開啟日志,開啟on,關閉off #負載均衡配置 upstream test.com { ip_hash; server 192.168.68.9:8080; server 192.168.68.72:8080; } server { listen 80; #監聽的端口,http默認監聽端口為80 server_name localhost; #監聽的主機名 location / { #設置請求的頭部中主機名為請求的主機名,而不是代理的nginx的主機名 proxy_set_header Host $host:$server_port; #代理的目標地址,如果要進行負載均衡,目標地址可添test.com proxy_pass http://192.168.68.9:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #https配置,https默認監聽端口為443 server { listen 443; server_name system.test.com; ssl on; ssl_certificate_key cert/system.key; #ssl key文件的位置,此處使用配置文件的相對路徑 ssl_certificate cert/system.pem; #證書文件,此處為阿里雲雲盾證書生成的.pem也可修改擴展名為熟悉的.crt 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 / { proxy_pass http://192.168.68.9:8080; } } #以下為我的fdfs文件配置,沒有使用fdfs可以不用配置 server { listen 9300; server_name localhost; #location /group1/M01 { # root /home/fdfs/storage2/data; # ngx_fastdfs_module; #} location /group1/M00 { root /home/fdfs/storage1/data; ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #include vhost/*.conf; }
關於https配置ssl
如果在安裝nginx的時候沒有安裝 --with-http_ssl_module模塊要先安裝該模塊
nginx –V 可查看已經安裝的模塊
如果沒有安裝,只需執行以上步驟中的2、3步進行手動添加ssl模塊
添加一個https的server大概如下面這個樣子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
server {
listen 443; ##默認的監聽端口為443
server_name localhost;
ssl
on
;
ssl_certificate_key xxx.key; ##私鑰
ssl_certificate xxx.crt; ##證書,證書中包含公鑰和私鑰加密后的簽名信息
location / {
root html;
index index.html index.htm;
proxy_pass http:
//xxx.xxx.xxx.xxx:xxx;
}
}
|
私鑰和公鑰為非對稱加密方式加密內容,即公鑰加密后的內容只有私鑰可解,私鑰加密后的內容只有公鑰可解;
大概原理:
服務器證書中包含公鑰和簽名(對證書內容進行hash后使用ca機構的私鑰加密)等信息,瀏覽器請求發出tcp三次握手成功后服務器會將該證書發送給瀏覽器,瀏覽器會判斷服務器證書是否過期或被吊銷且是權威機構頒發,不是符合條件會中斷訪問,並顯示警告提示;如果符合的話會產生一個隨機字符串並用服務器證書中的公鑰加密發送給服務器端,服務器再通過自己的私鑰解密那個隨機字符串,將這個字符串作為加密的密碼來進行對稱加密之后與瀏覽器交互的數據;
關於瀏覽器怎樣判斷服務器證書是否為權威機構頒發:
首先要知道一下幾個事情
服務器證書是受信任的ca機構簽發的
ca機構本身也是有證書的(存在公鑰和私鑰)
瀏覽器本身內置了受信任的ca機構的證書(有公鑰)
服務器證書當中存在"簽名"
證書中的"簽名"是對服務器證書內容進行hash后使用ca機構的私鑰加密形成的
當客戶端接收到了服務器證書之后,會根據證書的內容進行散列計算得到證書內容的hash值,還會通過瀏覽器內置的證書(ca)提供的公鑰對簽名進行解密.
如果解密得到的內容等於前面散列出的hash值,則說明證書是權威機構頒發的
原文來源:https://www.cnblogs.com/luobiao320/p/7189934.html