RedHat AS4 Update7 安裝nginx及配置前端代理
http://download.chinaunix.net/
下載以下3個工具
pcre-8.02.tar.bz2
zlib-1.2.7.tar.gz
openssl-1.0.1a.tar.gz
注:
pcre-8.02.tar.bz2、zlib-1.2.7.tar.gz和openssl-1.0.1a.tar.gz是安裝nginx所需的庫
http://www.nginx.org/
nginx-1.2.8.tar.gz 穩定版(目前官方最高版本)
=============================================================
注:nginx-1.3.15.tar.gz 開發版(奇發偶穩,主要看第2位)
=============================================================
------------------------------------------------------------------------------------------------------------
一般在安裝完系統後,爲了避免《系統安全掃描》出現OpenSSH漏洞,所以在進行OpenSSH升級時就已經安裝了這兩個版本。
------------------------------------------------------------------------------------------------------------
一、 升級zLib至1.2.7版本
安裝zlib-1.2.7
# tar –zxvf zlib-1.2.7.tar.gz
# cd zlib-1.2.7
# ./configure --prefix=/usr --shared 2>config.error
# make &&make install 2>make.error
二、 升級OpenSSL至1.0.1a版本
1. 安裝openssl-1.0.1
# tar –zxvf openssl-1.0.1a.tar.gz
# cd openssl-1.0.1a
# ./config shared zlib >config.error
# make &&make install >make.error
# mv /usr/bin/openssl /usr/bin/openssl.OFF
# mv /usr/include/openssl /usr/include/openssl.OFF
# ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
# ln -s /usr/local/ssl/include/openssl /usr/include/openssl
2. 配置庫文件搜索路徑
# echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
# ldconfig -v
3. 查看OpenSSL的版本號,驗證安裝結果
# openssl version -a
以上兩步如果已經做過了,那麼在此是可以省略掉的。
注:
當然,如果此臺機器原來rpm已經安裝過zlib和openssl了,而且不需要升級OpenSSH,那也可以直接從第三步開始即可。
------------------------------------------------------------------------------------------------------------------
三、安裝pcre-8.02
# tar xjf pcre-8.02.tar.bz2
# cd pcre-8.02
# ./configure --with-pcre=/usr/local/nginx/pcre-8.02/ 2>config.error
# make &&make install 2>make.error
四、安裝Nginx-1.2.8
# tar xzf nginx-1.2.8.tar.gz
# cd nginx-1.2.8
# ./configure --prefix=/usr/local/nginx/ --with-pcre=/usr/local/nginx/pcre-8.02/ --with-http_gzip_static_module --with-http_stub_status_module 2>config.error
說明:
--with-http_stub_status_module 用來啓用Nginx的NginxStatus功能,以監控Nginx的當前狀態
--with-http_gzip_static_module 啓用壓縮傳輸功能
##現網編譯安裝的時候可以加入以下兩個參數##
--with-cc-opt='-O3' --with-cpu-opt=athlon (說明:爲特定的CPU指定CPU類型編譯優化)
##這幾個參數可以在nginx解壓包的auto/cc/gcc文件裏面有,但默認是--with-cc-opt='-O' --with-cpu-opt=CPU ##
# make &&make 2>make.error
五、配置Nginx代理功能
# cd /usr/local/nginx/conf
# vi nginx.conf
[root@my conf]# cat nginx.conf
##這部分是全局配置##
user nobody; ##以nobody用戶來運行該程序,nobody默認是不允許登錄系統的,安全##
worker_processes 4; ##這個是開戶的進程數,最好是設置得接近CPU的核心數,每個進程平均消耗10~12MB內存##
error_log logs/error.log; ##錯誤日誌輸出##
#error_log logs/error.log notice; ##警告日誌輸出##
#error_log logs/error.log info; ##詳細錯誤日誌輸出##
worker_rlimit_nofile 65535; ##用於綁定worker進程和CPU,Linux內核2.4以上可用##
pid logs/nginx.pid; ##nginx運行的進程號##
events {
use epoll; ##對於Linux系統,首選epoll高效工作模式##
worker_connections 65536;
##此爲每個進程的最大連接數,max_client=worker_processes*worker_connections##
##做反向代理時變爲:max_client=worker_processes*worker_connections/4##
}
##這部分是HTTP服務器配置##
http {
include mime.types;
include proxy.conf; ##代理的全局配置文件,在這裏寫了之後就不用在每個代理節點再重複地寫入,具體內容見文章末尾##
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; ##開啓這項用於防止網絡阻塞##
server_tokens off; ##隱藏Nginx版本號信息,安全##
#keepalive_timeout 0;
client_header_buffer_size 1k; ##允許客戶端請求頭headerbuffer大小##
large_client_header_buffers 4 32k; ##客戶端請求中較大的消息頭的緩存最大數量和大小##
keepalive_timeout 60; ##會話連接保持時間##
client_header_timeout 3m; ##客戶端請求頭讀取時間##
client_body_timeout 3m; ##客戶端請求主體讀取時間##
send_timeout 3m; ##響應客戶端的超時時間##
##這部分是HttpGzip模塊配置##
gzip on; ##開啓實時壓縮輸出數據流##
gzip_http_version 1.1; ##HTTP協議版本,默認是1.1##
gzip_comp_level 2; ##壓縮比例,9壓縮比例最大,傳輸速度快,但處理較慢且大最消耗CPU##
gzip_proxied any;
gzip_types application/x-javascript text/plain text/css application/xml;
gzip_min_length 1100;
gzip_buffers 4 8k;
##這部分是負載均衡配置##
##定義一個負載均衡組www.test.com,然後與lcation部分通過"proxy_pass http://www.test.com"實現負載調度功能##
# upstream www.test.com {
# ip_hash; ##來自同一IP的訪問可以固定到後臺的某臺服務響應,這可以有效解決動態網頁存在的session共享問題##
##默認不加ip_hash這一行的話是採用詢的方式,即每個請求按時間順序逐一分配到不同的後端服務器,如果後端某臺服務器宕機,故障系統被剔除,使用戶訪問不受影響##
# server 192.168.1.100:8080;
# server 192.168.1.101:8081 weight=3 max_fails=3 fail_tomeout=20s; ##weight指定輪詢值,值越大,分配到的訪問機率越高,主要用於不周性能的服務器##
# server 192.168.1.102:8082 max_fails=3 fail_tomeout=20s; ##max_fails允許請求失敗的次數,默認爲1.fail_tomeout經歷max_fails次敗後,暫停服務的時間##
# server 192.168.1.106:8083 down; ##down表示當前server暫時不參與負載均衡##
# server 192.168.1.107:8084 backup; ##預留的備份機器,當其他所有非backup機器出現故障或者忙的時候,纔會請求backup機器,因爲這臺服務器壓力最輕##
# }
##這部分是server虛擬主機配置##
server {
listen 80;
server_name www.test.com; ##指定IP或域名,多個域名用空格分開##
charset gb2312; ##用於設置網頁的默認編碼格式##
access_log logs/www.testj.log main; ##虛擬主機的訪問日誌路徑,main用於指定日誌輸出的格式##
##這部分是代理配置,代理配置文件proxy.conf已經在全局的時候另指路徑##
location / {
proxy_pass http://192.168.1.100:8080;
}
##這部分是URL匹配配置,注:location支持正則表達式匹配##
location ~ ^/zd {
access_log logs/www.zd.log main;
proxy_pass http://192.168.1.100:8080;
}
#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;
}
##這部分是Nginx服務狀態,暫時不啓用##
# location ~ ^/ServerStatus/ {
# stub_status on;
# access_log off;
# }
#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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
[root@my conf]#
[root@my conf]# /usr/local/nginx/sbin/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@my sbin]# ./nginx 啓動Nginx,可以考慮加爲系統服務並開機啓動
[root@my sbin]# netstat -tnlp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29634/nginx
[root@my sbin]#/usr/local/nginx/sbin/nginx -s reload 不間斷服務重啓Nginx,即重新加載配置文件
=====================proxy.conf配置文件內容===========================
proxy_redirect off;
proxy_set_header Host $host;
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 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
======================================================================
nginx.conf和proxy.conf這兩個配置文件,在附件中有,有需要的可以下載看一下。
#cat nginx.conf |grep -v '^#' >nginx.conf.bak
這樣子就得到一個更清晰的配置文件了
後臺直接啓用一個簡單的Tomcat網站就可以進行驗證了。
提示:有什麼不足之處,有興趣的朋友可以加本人QQ羣:181169906 一起學習研究一下:)