Nginx---筆記五

Nginx的HTTPS服務:
爲何須要?
1.傳輸數據被中間人盜用,信息泄露
2.數據內容劫持、篡改
HTTPs協議的實現:
1.對傳輸內容進行加密以及身份驗證
對稱加密和非對稱加密:
對稱html

非對稱加密:node



2.HTTPS加密協議原理:nginx



3.中間人僞造客戶端和服務端:git

如何解決中間人劫持呢?
CA簽名證書:
客戶端對數字證書進行CA校驗:
1.若是檢驗成功則利用公鑰加密
2.若是校驗失敗則中止會話
4.生成密鑰和CA證書:
# 確認安裝openssl
>> openssl version
>> rpm -qa|grep open
# 確認Nginx已經編譯http_ssl_module
>> nginx -V
>> cd /etc/nginx
>> mkdir ssl_key
>> cd ssl_key
步驟1.生成key密鑰
>> openssl genrsa -idea -out wgw.key 1024
>> ls
步驟2.生成證書籤名請求文件(csr文件)
>> openssl req -new -key wgw.key -out wgw.csr
步驟3.生成證書籤名文件(CA文件)
>> openssl x509 -req -days 3650(若是不寫的話默認一個月到期) -in wgw.csr -signkey wgw.key -out wgw.crt
5.Nginx的HTTPs語法配置:
Syntax:ssl on | off;
Default: ssl off;
Context: http,servergithub

Syntax:ssl_certificate file;
Default: ---;
Context: http,server算法

Syntax:ssl_certificate_key file;
Default: ----;
Context: http,servervim

eg:
>> cd conf.d/
>> vi test_httos.conf緩存

server {
listen 443;
server_name 192.168.205.10 phantom.wgw.io;

ssl on;
ssl_certificate /etc/nginx/ssl_key/wgw.crt;
ssl_certificate_key /etc/nginx/ssl_key/wgw.key;tomcat

index index.html index.htm;

location / {
root /opt/LearningNginx/app/code;
}服務器

}

>> nginx -tc /etc/nginx/nginx.conf
>> nginx -c /etc/nginx/nginx.conf
>> nginx -s reload
>> netstat -luntp|grep 443 # 查看本地是否啓用了443
6.場景-配置蘋果要求的證書
6.1 服務器全部的鏈接使用TLS1.2以上版本(openssl 1.0.2)
>> openssl version
6.2 HTTPS證書必須使用SHA256以上哈希算法簽名
>> openssl x509 -noout -text -in ./wgw.crt
6.3 HTTPS證書必須使用RSA 2048位或ECC 256位以上公鑰算法
6.4 使用前向加密技術

>> openssl genrsa -idea -out phantom.key 1024
附加;如何生成一個去掉密碼保護碼的key?
>> openssl -rsa -in ./phantom.key -out ./phantom_nopass.key
>> openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout phantom.key -out phantom_apple.crt
>> vi test_https.conf
server {
listen 443 ssl;
resolver 8.8.8.8;
server_name localhost phantom.wgw.io;

ssl_certificate /etc/nginx/ssl_key/phantom_apple.crt;
ssl_certificate_key /etc/nginx/ssl_key/phantom.key;

index index.html index.htm;
location / {
root /etc/nginx/conf.d;
}

}
>> netstat -luntp | grep 443

7.HTTPS服務優化
方法一:激活keepalive長鏈接
方法二:設置ssl session緩存
eg:
server {
listen 443 ssl;
resolver 8.8.8.8;
server_name localhost phantom.wgw.io;

keepalive_timeout 100;

ssl_session_cache shared:SSL:10m; # 大約能夠存8000-10000個session會話
ssl_session_timeout 10m;


ssl_certificate /etc/nginx/ssl_key/phantom_apple.crt;
ssl_certificate_key /etc/nginx/ssl_key/phantom.key;

index index.html index.htm;
location / {
root /etc/nginx/conf.d;
}
}

 

 

Nginx與lua開發:
1.lua基礎語法:
1.1 lua:
是一個簡潔、輕量、可擴展的腳本語言
1.2 Nginx+lua優點:
充分的結合Nginx的併發處理epoll優點和Lua的輕量實現簡單的功能且高併發的場景
1.3 語法:
1.3.1安裝:
yum install lua
1.3.2 語法:
交互式:
>> lua
> print("hello world")
腳本:test.lua
# !/usr/bin/lua
print("hello world")
運行:
>> chmod a+rx ./test.lua
>> ./test.lua
註釋:
--行註釋
--[[
塊註釋
]]
變量:
a = 'wgw\n123"'
a = "wgw\n123""
a = '\971o\10\04923'
a = [[alo123"]]
布爾類型:只有nil-->空
false --->false
數字0或者空字符串--->都是true
lua變量沒有作特殊說明,全是全局變量
while循環:
sum = 0
num = 1
while num <= 100 do
sum = sum + num
num = num + 1
end
print("sum=",sum)
注:lua不支持++,或+=這樣的操做
for循環:
sum = 0
for i=1,100 do
sum = sum + i
end
if-else判斷語句:
if age == 40 and sex == "Male" then
print("大於40的男人")
elseif age > 60 and sex ~="Female" then
print("非女人並且大於60")
else
local age = io.read() 
--[[io.read:表示從屏幕的終端讀取用戶的輸入信息,
io.write:讀取輸出的信息,
~=:表示不等於,
字符串拼接:".."
io庫的分別從stdin和stdout讀寫的read和write函數]]

print("your age is"..age)
end

2.nginx 與lua開發
1.LuaJIT
>> wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
>>make install PREFIX=/usr/local/LuaJIT
>>export LUAJIT_LIB=/usr/local/LuaJIT/lib
>>export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
2.ngx_devel_kit和lua-nginx-module
>> cd /opt/download
>> wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
>> wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
3.從新編譯Nginx
>> cd /opt/download
>> wget http://nginx.org/download/nginx-1.12.1.tar.gz
>> ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/ngx_devel_kit-0.3.0 --add-module=/opt/download/lua-nginx-module-0.10.9rc7
>> make -j 4 && make install

四、加載lua庫,加入到ld.so.conf文件
>>echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
而後執行以下命令:
>>ldconfig
5.nginx調用lua指令
Nginx的可插拔模塊化加載執行,共11個處理階段
set_by_lua ,set_by_lua_file(後面加lua腳本文件):
設置nginx變量能夠實現複雜的複製邏輯
access_by_lua,access_by_lua_file:
請求訪問階段處理,用於訪問控制
content_by_lua,content_by_lua_file:
內容處理器,接收請求處理並輸出響應
6.NGINX lua API:
ngx.var :nginx變量
ngx.req.get_headers:獲取請求頭
ngx.req.get_uri_args:獲取url請求參數
ngx.redirect:重定向
ngx.print:輸出響應內容體
ngx.say:通ngx.print,可是會最後輸出一個換行符
ngx.header:輸出響應頭
...

 

3.場景:用nginx結合lua實現代碼的灰度發佈

灰度發佈:
按照必定的關係區別,分部分的代碼進行上線,使代碼的發佈能平滑過渡上線
3.1 用戶的信息cookie等信息區別
3.2 根據用戶的IP地址
3.2.1 如圖:


3.2.2 安裝memcached
>> yum install memcached
3.2.3 啓動tomcat
>> mv tomcat tomcat8080
>> cp tomcat8080 tomcat9090
>> cd tomcat9090/conf
>> vi server.xml # 修改端口
>> sh catalina.sh start;tail -f ../logs/catalina.out # 啓動這兩個tomcat 
>> nestat -luntp
3.2.4啓動memcached
>> memcached -p11222 -u nobody -d
>> netstat -luntp|grep 11222 
3.2.5 lua腳本
>> wget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gz
>> tar -zxvf v0.11.tar.gz
>> cp -r lua-resty-memcached-0.11/lib/resty /usr/local//share/lua/5.1/
>> cd /etc/nginx/conf.d
>> vim dep.conf

server {
listen 80;
server_name localhost phantom.wgw.io;
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,lua")';
}

location /myip {
default_type 'text/plain';
content_by_lua '
clientIP = ngx.req.get_headers()["x_forwarded_for"]
ngx.say("IP:",clientIP)
';
}

location / {
default_type "text/html";
content_by_lua_file /opt/LearnNginx/app/lua/dep.lua;
#add_after_body "$http_x_forwarded_for";
}

location @server {
proxy_pass http://127.0.0.1:9090;
}

location @server {
proxy_pass http://127.0.0.1:8080;
}

error page 500 502 503 504 404 /50x.html;location = /50x.html{root /usr/share/nginx/html;}}>> telnet 127.0.0.1 11222set 192.168.205.10 0 0 11storedget 192.168.205.10>>

相關文章
相關標籤/搜索