Nginx安裝部署與測試

場景:項目須要部署在生產環境中,這些新的工具都須要在生產環境中去實踐練習。有時間再部署一套ELK的日誌分析系統,這樣的系統纔算具備必定的應用價值。javascript

1 Nginx安裝

用root用戶安裝,採用源代碼編譯的方式來進行安裝,正式開始前,請確認gcc、g++開發庫之類的已經預先安裝好php

------------------------------------------------------------------------------------------------css

先把nginx安裝要用到的全部文件(「軟件/nginx-1.8.0 下面的全部文件」)上傳至服務器/root/nginx目錄(沒有該目錄則新建)html

通常咱們都須要先裝pcre,zlib,前者用於url rewrite,後者用於gzip壓縮,openssl用於後續可能升級到https時使用java

首先切換到nginx目錄linux

cd /root/nginx

 1.1 安裝pcre

tar -zxvf pcre-8.36.tar.gz

cd pcre-8.36/

./configure

make

make install

 

1.2 安裝zlib

tar -zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8/

./configure

make

make install

 

1.3 安裝openssl

tar -zxvf openssl-1.0.1c.tar.gz

cd openssl-1.0.1c/

./config

make

make install

 

1.4 安裝nginx

tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0/

#這裏要根據本身安裝軟件的版本填寫
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=../pcre-8.36 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1c

make

make install

 

1.5  測試nginx的啓動與關閉是否正常

cd /usr/local/nginx/sbin
//啓動
./nginx


ps –ef|grep nginx

//關閉進程
kill -QUIT 主進程號(上面ps命令看到的帶master字樣的進程號)

瀏覽器訪問如:http://168.33.130.234/
nginx

若是能正常顯示nginx首頁,則表示安裝成功,測試關閉web

 1.6 開機自啓動腳本

 ps:這裏看到不少清晰明白的nginx開機自啓動教程,可是死活都安裝不成功。最後詢問運維人員發現是系統不一致形成的。這裏我採用的suse系統,因此自啓動腳本的製做過程不太同樣。後端

常規自啓動過程:http://www.tuicool.com/articles/FBva2i瀏覽器

沒辦法,這裏只能另外尋求解決辦法:

1.添加nginx運行的用戶組:
[root@localhost nginx-1.0.15]# useradd -s /sbin/nologin nginx
 
2.Nginx默認安裝在/usr/local/nginx目錄下,爲了方便應用,能夠添加一個nginx主程序的符號連接:
[root@localhost nginx-1.0.15]# ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin
 ps:這裏和自啓動腳本中的啓動鏈接相對應,不能省略
3.使用nginx -t命令檢查nginx配置文件是否有語法錯誤:
[root@linux nginx-1.0.15]# nginx -t
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
[root@linux nginx-1.0.15]#
 執行nginx -t後出現上述提示 表示配置文件語法正確
 
4.使用nginx啓動服務,而後使用netstat命令進行查看:
[root@linux nginx-1.0.15]# netstat -anpt|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16787/nginx         
[root@linux nginx-1.0.15]#
5.nginx啓動成功後,能夠在瀏覽器中查看初始的web頁面:

在客戶端瀏覽器中執行:http://10.0.0.133(服務器IP地址)進行查看:

另外在服務器命令行下使用文本瀏覽器工具elink進行查看:

[root@localhost nginx-0.8.54]# elinks http://10.0.0.133

6.使用系統信號控制nginx進程:
啓動:nginx
重啓:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

[root@localhost~]# kill -s HUP nginx   //從新加載配置文件,等同於「killall -1 nginx」
[root@localhost~]# kill -s QUIT nginx  //安全退出,等同於「kill -3 nginx」
[root@localhost~]# kill -s TERM nginx //快速退出,不等待處理完當前鏈接
 有個自啓動的腳本操做起來方便不少:
另外,爲了方便管理,能夠添加一個nginx服務腳本,使用chkconfig和service命令管理nginx服務:
在nginx安裝目錄下 vi nginx
#!/bin/bash
#description: Nginx Service Control Script
case "$1" in
  start)
         /usr/sbin/nginx    
         ;;
  stop)
         /usr/bin/killall -s QUIT nginx
            ;;
  restart)
         $0 stop
         $0 start
         ;;
  reload)
         /usr/bin/killall -s HUP nginx
         ;;
  *)
    echo "Usage:$0 {start|stop|restart|reload}"
    exit 1
esac
exit 0

 

保存後複製「/root/nginx/nginx」腳本到/etc/init.d下

cp nginx /etc/init.d

[root@localhost~]# chmod a+x /etc/init.d/nginx    爲nginx腳本賦予可執行權限
[root@localhost~]# chkconfig --add nginx
[root@localhost~]# chkconfig --level 2345 nginx on

接下來就可使用service nginx stop|start|restart|reload對nginx服務進行控制:

[root@linux nginx-1.0.15]# service nginx restart

[root@linux nginx-1.0.15]# !nets
netstat -anpt|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16787/nginx         
[root@linux nginx-1.0.15]#

2 niginx配置

2.1 配置文件詳解

以root用戶登陸ssh

cd /usr/local/nginx/conf/
vi nginx.conf
#修改配置
worker_processes  4;

events {
    use epoll;
    worker_connections  65535;
}
#生產虛擬機爲4個物理cpu,32GB內存,使用epoll事件模型,最大鏈接數爲65535
#在http{}裏面配置增長
client_max_body_size 200m;
client_header_buffer_size 16k;
large_client_header_buffers 4 64k;

keepalive_timeout  900;
gzip  on;
gzip_types text/plain application/x-javascript text/css application/xml;

#負載均衡配置
upstream opss.web{
    ip_hash;
server 168.11.209.37:80;
server 168.11.209.42:80;
}

#在server{}裏面增長配置
location ^~ /opssweb/ {
proxy_pass http://opss.web;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

 

配置也很簡單,upstream中配置輪詢策略和服務器的ip、端口(端口能夠去tomcat的server.xml中查看),綠色部分隨意命名,只要一隻便可。

upstream 那裏隨便命名 和下面server裏面的proxy_pass名稱對應上就能夠了。這裏請參考問題3.2中的分析

nginx經過ip和端口 會自動找到tomcat部署的應用程序 ,與server.xml中的<Context path= 配置無關。

這裏附上本身的配置示例nginx.conf:

#user  nobody;
worker_processes  8;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  65535;

}


http {
    include       mime.types;
    default_type  application/octet-stream;

    client_max_body_size 200m;
    client_header_buffer_size 16k;
    large_client_header_buffers 4 64k;


    #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  900;
    gzip_types text/plain application/x-javascript text/css application/xml;

    gzip  on;
    upstream nginxweb{
     ip_hash; #輪詢側率(若是省略就執行輪換輪詢)
     #這裏的ip和端口隨意添加,和tomcat中的應用保持一隻  server
168.33.131.126:8088; server 168.33.130.234:8088; } 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; #} location ^~ /lfcpweb/ { proxy_pass http://nginxweb; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } # 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; # } #} }

 

配置完成後,只要server端的應用正常啓動,就能夠經過

http://168.33.130.234/lfcpweb/login來訪問

server 168.33.131.126:8088;
server 168.33.130.234:8088;兩臺服務器上的應用,這裏我是經過改變輪詢側率,而後在兩臺服務器上查看日誌打印來肯定nginx可以正常的工做

2.2 配置日誌切割

1)              配置nginx日誌自動切割

 

第一步就是重命名日誌文件,不用擔憂重命名後nginx找不到日誌文件而丟失日誌。在你未從新打開原名字的日誌文件前,nginx仍是會向你重命名的文件寫日誌,linux是靠文件描述符而不是文件名定位文件。

第二步向nginx主進程發送USR1信號。

nginx主進程接到信號後會從配置文件中讀取日誌文件名稱,從新打開日誌文件(以配置文件中的日誌名稱命名),並以工做進程的用戶做爲日誌文件的全部者。

從新打開日誌文件後,nginx主進程會關閉重名的日誌文件並通知工做進程使用新打開的日誌文件。

 

工做進程馬上打開新的日誌文件並關閉重名名的日誌文件。

創建日誌文件/usr/local/nginx/nginx_log.sh

而後你就能夠處理舊的日誌文件了。vi nginx_log.sh

#nginx clean access.log and error.log

#!/bin/bash

#設置日誌文件存放目錄          

logs_path="/usr/local/nginx/logs/"

#設置pid文件    

pid_path="/usr/local/nginx/nginx.pid"


#重命名日誌文件       
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
mv ${logs_path}error.log ${logs_path}error_$(date -d "yesterday" +"%Y%m%d").log

 

#向nginx主進程發信號從新打開日誌             
kill -USR1 `cat ${pid_path}`

#刪除7天之前的日誌文件
find ${logs_path} -mtime +7 -name '*.log' -exec rm -f {} \;

 

保存以上腳本nginx_log.sh

crontab 設置做業

0 0 * * * bash /usr/local/nginx/nginx_log.sh

這樣就天天的0點0分把nginx日誌重命名爲日期格式,並從新生成今天的新日誌文件。

2.3 日誌格式設置

在nginx接收到請求以後, 需把請求分發到後端WEB服務集羣. 在這裏須要記錄分發日誌, 來分析後端每臺WEB服務器處理的請求數目.

對日誌輸出格式進行以下設置便可:

http {  
log_format  main    
  ' $remote_user [$time_local]  $http_x_Forwarded_for $remote_addr  $request '  
 '$http_x_forwarded_for '                       
 '$upstream_addr '                        
 'ups_resp_time: $upstream_response_time '                        
 'request_time: $request_time';  
  
access_log  logs/access.log  main;  
  
server{}  
...  
} 

在日誌顯示的信息爲:

- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.010 request_time: 0.011  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.16:8188 ups_resp_time: 0.006 request_time: 0.006  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.013 request_time: 0.013  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.17:8188 ups_resp_time: 0.003 request_time: 0.003  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.18:8188 ups_resp_time: 0.004 request_time: 0.004  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.012 request_time: 0.013  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.18:8188 ups_resp_time: 0.005 request_time: 0.005  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.16:8188 ups_resp_time: 0.011 request_time: 0.011  
- [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.447 request_time: 0.759 

3 問題分析

3.1 輪詢策略

nginx 報failed (13: Permission denied),最終發現是採用默認輪詢策略,致使session丟失,程序訪問失敗。

主要是程序的驗證碼,兩次訪問不一致,就沒法訪問。觀察程序發現後臺是經過session獲取驗證碼。

#負載均衡配置

upstream lfcpweblist{

        ip_hash;

        server 168.33.130.113:8088;

        server 168.33.130.114:8088;

    }

ps:這裏輪詢策略採用ip_hash;的策略, 每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。

3.2 Nginx配置proxy_pass轉發的/路徑問題

由於路徑問題,折騰了很久都沒解決nginx中的404錯誤,慚愧~

 

Nginx配置proxy_pass轉發的/路徑問題

在nginx中配置proxy_pass時,若是是按照^~匹配路徑時,要注意proxy_pass後的url最後的/,

當加上了/,至關因而絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;

若是沒有/,則會把匹配的路徑部分也給代理走。

location ^~ /static_js/ 

proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
proxy_pass http://js.test.com/
}

如上面的配置,若是請求的url是http://servername/static_js/test.html
會被代理成http://js.test.com/test.html

而若是這麼配置

location ^~ /static_js/ 

proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
proxy_pass http://js.test.com
}

則會被代理到http://js.test.com/static_js/test.htm

固然,咱們能夠用以下的rewrite來實現/的功能

location ^~ /static_js/ 

proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
rewrite /static_js/(.+)$ /$1 break; 
proxy_pass http://js.test.com

1

相關文章
相關標籤/搜索