Nginx (發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。 其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好.目前中國大陸使用nginx網站用戶有:新浪、網易、 騰訊,另外知名的微網誌Plurk也使用nginx。
首先是window環境結構如下圖:
Window xp環境:Nginx1.0.15+Tomcat7.0.8
1、下載地址
http://nginx.org/en/download.html
2、目錄結構
Nginx-
|_ conf 配置目錄
|_ contrib
|_ docs 文檔目錄
|_ logs 日誌目錄
|_ temp 臨時文件目錄
|_ html 靜態頁面目錄
|_ nginx.exe 主程序
解壓縮到一個無空格的英文目錄即可
雙擊nginx.exe即可啓動Nginx服務器
DOS環境啓動
運行CMD 到DOS界面
下面是一些常用的使用命令:
Nginx -s stop 快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。
Nginx -s quit 平穩關閉Nginx,保存相關信息,有安排的結束web服務。
Nginx -s reload 因改變了Nginx相關配置,需要重新加載配置而重載。
Nginx -s reopen 重新打開日誌文件。
Nginx -v 查看版本
Nginx -V 查看nginx的版本,編譯器版本和配置參數
3、nginx.conf配置
Nginx配置文件默認在conf目錄,主要配置文件爲nginx.conf,我們安裝在D:\nginx-1.0.15、默認主配置文件爲D:\nginx-1.0.15\nginx.conf。下面是nginx作爲前端反向代理服務器的配置。
Nginx.conf代碼
#Nginx所用用戶和組,window下不指定
#user Administrator;
#工作的子進程數量(通常等於CPU數量或者2倍於CPU)
worker_processes 2;
#錯誤日誌存放路徑
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定pid存放文件
#pid logs/nginx.pid;
events {
#使用網絡IO模型linux建議epoll,FreeBSD建議採用kqueue,window下不指定。
#use epoll;
worker_connections 2048;
}
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;
include gzip.conf;
upstream localhost {
#根據ip計算將請求分配各那個後端tomcat,許多人誤認爲可以解決session問題,其實並不能。
#同一機器在多網情況下,路由切換,ip可能不同
#ip_hash;
server localhost:18081;
server localhost:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
}
#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;
# }
#}
}
代理設置如下:
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 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
zip壓縮相關配置如下:
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript;
4、Tomcat配置
第一處端口修改:
<!-- 修改port端口:8006 倆個tomcat不能重複,端口隨意,別太小-->
<Server port="8006" shutdown="SHUTDOWN">
第二處端口修改:<!-- port="18081" tomcat監聽端口,隨意設置,別太小 -->
<Connector port="18081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
第三處端口修改:<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Engine元素增加jvmRoute屬性:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
啓動Nginx和倆個Tomcat驗證置負載均衡設置,http://localhost/ 或http://localhost/index.jsp 。
至此window下nginx+tomcat負載均衡配置結束,關於tomcat Session的問題通常是採用memcached,或者採用nginx_upstream_jvm_route ,他是一個 Nginx 的擴展模塊,用來實現基於 Cookie 的 Session Sticky 的功能。如果tomcat過多不建議session同步,server間相互同步session很耗資源,高併發環境容易引起Session風暴。請根據自己應用情況合理採納session解決方案。
參考資料:
http://www.oschina.net/bbs/thread/9301
oschina.net 生產配置,此網站採用java語言,nginx,tomcat服務器。
張宴:<<實戰Nginx>>