前言
新書Java併發程式設計系統與模型已上線,歡迎拜讀。
由於微信小程式要使用Https,但是又不能修改已有線上的配置。所以最簡單的方法就是使用nginx轉發,在nginx上使用https,然後再轉發到內部伺服器。Nginx由於其優良的效能。一臺4核16GB的記憶體完全可以支撐日均百萬pv級別的訪問。
基礎知識
Nginx由於使用了 epoll模型,要求linux的核心必須在2.6以上。要了解epoll模型,可以看看知乎上的這篇文章IO多路複用與 select,poll與epoll的關係。
使用 uname -a
檢視Linux 核心版本,如下是Centos 6.5的顯示:
Linux VM_26_145_centos 2.6.32-504.30.3.el6.x86_64 #1 SMP Wed Jul 15 10:13:09 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux複製程式碼
下載
Nginx 的官網的下載地址:nginx.org/en/download…
Nginx官網提供了三個型別的版本:
- Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
- Stable version:最新穩定版,生產環境上建議使用的版本
- Legacy versions:遺留的老版本的穩定版
編譯與安裝
nginx依賴以下模組:
- gzip模組需要 zlib 庫 及其開發環境
- rewrite模組需要 pcre 庫及開發環境
- ssl 功能需要openssl庫及開發環境以及 yum install -y gcc-c++ 環境。
以gzip 模組為例,檢視以下模組是否安裝:
rpm -qa |grep zlib複製程式碼
如果沒有安裝,那麼就 yum install zlib zlib-devel
。
make是用來編譯的,它從Makefile中讀取指令,然後編譯。make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。
最簡單的編譯安裝 Nginx
tar zxvf nginx-1.10.2.tar.gz
解壓以後進入到
[root@VM_26_145_centos nginx-1.10.2]# ./configure
[root@VM_26_145_centos nginx-1.10.2]# make
[root@VM_26_145_centos nginx-1.10.2]# make install複製程式碼
./configure 是用來檢查本機的的安裝環境。在configure階段結束以後,將會出現如下資訊:
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"複製程式碼
可以看到預設的安裝目錄以及一些基本的配置。
啟動
nginx預設採用80埠,在直接啟動nginx之前,先檢查80埠是否被佔用,使用fuser -n tcp 80
或者netstat -pan | grep :80
檢視80埠是否被佔用。這裡假設沒有被佔用,然後進入 /usr/local/nginx
(上文提到的預設安裝目錄)目錄:
[root@VM_26_145_centos nginx]# sbin/nginx -c conf/nginx.conf複製程式碼
訪問:http://ip:80/就可以看到nginx的歡迎頁面。
nginx配置
在/usr/local/nginx/conf(預設配置)中,有一個nginx.conf檔案。nginx.conf的程式碼是這樣的:
# user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}複製程式碼
刪掉不必要的檔案,基本檔案型別是這個樣子:
# user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
}複製程式碼
注意到最頂上的日誌配置嗎?在頂部設定的配置全域性生效。但是子模組可以覆蓋它。頂部日誌配置:
error_log /disk/nginx/logs/error.log;
accsess_log 去掉 mian 。 main 表示的使用者自定義的日誌格式的名字。 目前並沒有設定。複製程式碼
假設開發人員改變了nginx.conf配置,測試nginx.conf是否合法:
[root@VM_220_53_centos nginx]# sbin/nginx -t -c conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful複製程式碼
nginx配置檔案架構的圖:
這裡有詳細的配置。
###https
在編譯階段需要附帶編譯上ssl模組:./configure --with-http_ssl_module
限流
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
server {
...
limit_req zone=perip burst=5 nodelay;
limit_req zone=perserver burst=10;
}複製程式碼
注意在Http中配置以後需要在server中引入。
burst一秒中可以訪問的資料量。burst相當於一個授權令牌,每秒中每次查詢,當前burst-1,查詢結束,burst+1;
如果burst為0時,訪問不了。
public class TestNginx {
@Test
public void testMobileIsUsed() {
for (int i = 0; i < 100; i++) {
HttpResponse response = HttpRequest.get("http://123.206.18.37:8088/").send();
if (response.statusCode() != 200) {
assertEquals(1, 0);
}
System.out.println(response.bodyText());
}
}
}複製程式碼
可以看到,基本上是1秒返回一次了。
例項配置:
#user nobody;
worker_processes 1;
error_log /disk/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /disk/nginx/logs/host.access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
# HTTPS server
server {
limit_req zone=perip burst=5 nodelay;//限流配置
limit_req zone=perserver burst=10;
listen 443;
server_name mp.baidu.com;
ssl on;
ssl_certificate 1_mp.baidu.com_bundle.crt;
ssl_certificate_key 2_mp.baidu.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
proxy_pass http://10.105.26.210; //直接轉發
}
}
}複製程式碼