1. 程式人生 > >十分鐘-Nginx入門到上線

十分鐘-Nginx入門到上線

rep dir 入門到 grep type orm som inux 編譯

前言

??由於微信小程序要使用Https,但是又不能修改已有線上的配置。所以最簡單的方法就是使用nginx轉發,在nginx上使用https,然後再轉發到內部服務器。Nginx由於其優良的性能。一臺4核16GB的內存完全可以支撐日均百萬pv級別的訪問。
基礎知識
??Nginx由於使用了 epoll模型,要求linux的內核必須在2.6以上。

使用 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 x

下載
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

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;  //直接轉發
    }
}

}

十分鐘-Nginx入門到上線