Nginx JSP安裝和使用的菜鳥手冊(cè)
本文主要講述Nginx JSP如何進(jìn)行安裝和使用,但是要怎樣創(chuàng)建Nginx JSP呢?這些內(nèi)容都是一些門戶網(wǎng)站和技術(shù)論壇找到的,中間可能有不少錯(cuò)誤是我沒有挑出的,歡迎大家指正。
為了確保能在Nginx JSP中使用正則表達(dá)式進(jìn)行更靈活的配置,安裝之前需要確定系統(tǒng)是否安裝有 PCRE(Perl Compatible Regular Expressions)包,rpm包和tar.gz都可以
Rpm包如下:
- pcre-6.6-1.1
- pcre-devel-6.6-1.1
- tar.gz包
- #wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
- #tar zxvf pcre-7.7.tar.gz
- #cd pcre-7.7
- # ./configure
- # make
- # make install
如果沒有的話會(huì)報(bào)類似如下錯(cuò)誤:
- ./configure: error: the HTTP rewrite module requires the PCRE library.
- You can either disable the module by using --without-http_rewrite_module
- option, or install the PCRE library into the system, or build the PCRE library
- statically from the source with nginx by using --with-pcre=<path> option.
2 安裝 Nginx JSP
- #wget http://sysoev.ru/nginx/nginx-0.7.14.tar.gz
- # tar zxvf nginx-0.7.14.tar.gz
- #cd nginx-0.7.14
- #./configure --prefix=/usr/local/nginx --with-http_stub_status_module
- #make
- #make install
說明:
參數(shù) --with-http_stub_status_modul是為了啟用nginx的 NginxStatus 功能,用來監(jiān)控 Nginx 的當(dāng)前狀態(tài)
--prefix=/usr/local/nginx 指定安裝目錄更詳細(xì)的參數(shù)參考./configure --help
安裝成功后 /usr/local/nginx 目錄下有四個(gè)子目錄分別是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一個(gè)程序文件位于 sbin 目錄下的 nginx 文件。確保系統(tǒng)的 80 端口沒被其他程序占用,運(yùn)行 sbin/nginx 命令來啟動(dòng) Nginx,打開瀏覽器訪問此機(jī)器的 IP,如果瀏覽器出現(xiàn) Welcome to nginx! 則表示 Nginx 已經(jīng)安裝并運(yùn)行成功。
關(guān)于Nginx JSP的幾個(gè)命令簡(jiǎn)單說明:
-c </path/to/config> 為 Nginx 指定一個(gè)配置文件,來代替缺省的。
-t 不運(yùn)行,而僅僅測(cè)試配置文件。nginx 將檢查配置文件的語法的正確性,并嘗試打開配置文件中所引用到的文件。
-v 顯示 nginx 的版本。
-V 顯示 nginx 的版本,編譯器版本和配置參數(shù)
3配置 nginx.conf
修改配置文件 /usr/local/nginx/conf/nginx.conf下面是一個(gè)配置給出些說明:
- user nobody nobody; #工作進(jìn)程的屬主
- worker_processes 2; # 工作進(jìn)程數(shù),一般與 CPU 核數(shù)等同
- error_log /usr/local/nginx/logs/nginx_error.log crit;
- pid /usr/local/nginx/nginx.pid;
- #Specifies the value for maximum file descriptors that can
be opened by this process.- worker_rlimit_nofile 51200;
- events
- {
- use epoll;
- worker_connections 51200; # 每個(gè)工作進(jìn)程允許最大的同時(shí)連接數(shù)
- }
- http
- {
- include mime.types;
- default_type application/octet-stream;
- charset gbk;
- server_names_hash_bucket_size 128;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- gzip on;
- # gzip_min_length 1k;
- # gzip_buffers 4 8k;
- # gzip_http_version 1.1;
- # gzip_types text/plain application/x-javascript text/css
text/html application/xml;- server
- {
- listen 80; #nginx偵聽端口
- server_name localhost;
- index index.jsp index.html index.htm ;
- root /usr/local/www;#web的工作目錄
- if (-d $request_filename)
- {
- rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
- }
- #加下面這段是因?yàn)槲野l(fā)行如果首頁是index.jsp的話好像不能默認(rèn)通過
index.jsp來解析- location / {
- root /usr/local/www;
- index index.jsp;
- proxy_pass http://localhost:8080;
- }
- #以擴(kuò)展名方式匹配靜態(tài)文件
- Location ~* \.(htm|html|gif|jpg|jpeg|png|bmp|ico|rar|css|
js|zip|java|jar|txt|flv|swf|txt|wma)$- {
- root /usr/local/www;
- expires 24h;
- }
- #以目錄方式匹配靜態(tài)目錄
- Location ~ ^/(images|common|cooperation|download|huodong|
inc|magic|manager|produces|pages|secert|SinaEditor|styles|
javascript)/- {
- root /usr/local/www;
- expires 30d;
- }
- #以擴(kuò)展名方式匹配動(dòng)態(tài)文件
- location ~* \.(jsp|do)$
- {
- root /usr/local/www;
- index index.jsp;
- include /usr/local/nginx/conf/proxy.conf; # 加載proxy.conf
也就是測(cè)試中用來鏈接JSP- proxy_pass http://localhost:8080;
- proxy_set_header X-Real-IP $remote_addr;
- }
- #讓我的nginx也支持php
- location ~ .*\.php?$
- {
- include fcgi.conf;
- #fastcgi_pass unix:/tmp/php-cgi.sock;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- }
- #注意如果開啟下面這幾段的話就不能有上文中l(wèi)ocation / {的部分否則會(huì)
報(bào)錯(cuò),也就是只能有一個(gè)location / {- # location / {
- # include /usr/local/nginx/conf/proxy.conf;
- # proxy_pass http://localhost:8080;
- # proxy_set_header X-Real-IP $remote_addr;
- # }
- log_format access '$remote_addr - $remote_user [$time_local]
"$request" '- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- access_log /usr/local/nginx/logs/access.log access;
- }
- server
- {
- listen 84;#讓nginx的NginxStatus功能運(yùn)行在我的84端口,當(dāng)然自己配
置對(duì)應(yīng)的server_name也可以- server_name localhost;
- location / {
- stub_status on;
- access_log off;
- }
- }
- }
關(guān)于include /usr/local/nginx/conf/proxy.conf; # 加載proxy.conf 也就是測(cè)試中用來鏈接JSP
Vi /usr/local/nginx/conf/proxy.conf
內(nèi)容如下:
- # 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_buffers 32 4k;
- 關(guān)于#讓我的nginx也支持php
- location ~ .*\.php?$
- {
- include fcgi.conf;
- #fastcgi_pass unix:/tmp/php-cgi.sock;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- }
這段不做詳細(xì)說明了:
啟動(dòng)Nginx JSP
測(cè)試配置文件有無問題,例如:
- #/usr/local/nginx/sbin/nginx –t
- 2008/09/09 12:16:13 [info] 17651#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- 2008/09/09 12:16:13 [info] 17651#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
上面說明配置文件沒有問題啟動(dòng)
- #/usr/local/nginx/sbin/nginx
查看是否啟動(dòng)了
- # ps fax|grep nginx
- 17966 pts/1 S+ 0:00 \_ grep nginx
- 17514 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
- 17515 ? S 0:00 \_ nginx: worker process
- 17516 ? S 0:00 \_ nginx: worker process
NginxStatus功能說明如下:
- http://192.168.1.145:84/
- Active connections: 9
- server accepts handled requests
- 14 14 150
- Reading: 0 Writing: 1 Waiting: 8
active connections -- 對(duì)后端發(fā)起的活動(dòng)連接數(shù)
server accepts handled requests -- nginx 總共處理了 14 個(gè)連接, 成功創(chuàng)建 14 次握手 (證明中間沒有失敗的), 總共處理了 150 個(gè)請(qǐng)求
reading -- nginx 讀取到客戶端的 Header 信息數(shù)。
writing -- nginx 返回給客戶端的 Header 信息數(shù)。
waiting -- 開啟 keep-alive 的情況下,這個(gè)值等于 active - (reading + writing),意思就是 Nginx 已經(jīng)處理完正在等候下一次請(qǐng)求指令的駐留連接
取服務(wù)器信息
- HTTP/1.1 200 OK
- Server: nginx/0.7.14
- Date: Tue, 09 Sep 2008 07:22:03 GMT
- Content-Type: text/html;charset=GBK
- Transfer-Encoding: chunked
- Connection: keep-alive
- Set-Cookie: JSESSIONID=049F77C0BE388C3C942A4A80EE1DA346; Path=/
以上就是對(duì)Nginx JSP的詳細(xì)介紹希望大家有所幫助。
【編輯推薦】