Nginx安裝部署與測試


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

1 Nginx安裝

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

------------------------------------------------------------------------------------------------

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

一般我們都需要先裝pcre,zlib,前者用於url rewrite,后者用於gzip壓縮,openssl用於后續可能升級到https時使用

首先切換到nginx目錄

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首頁,則表示安裝成功,測試關閉

 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM