nginx操作指南之二
四、java支持
nginx可以通過tomcat增加jsp支持。默認情況下,CentOS可能已經安裝java運行庫JDK(http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html),可以通過下面的方式將其升級為最新版本。
4.1 安裝jdk
先按上面的地址下載新版本的JDK文件,要注意查看系統對應的版本(區分32位和64位)然后輸入以下指令進行安裝配置,如下所示:
tar?zxvf?jdk-7u45-linux-x64.tar.gz mv?/root/jdk1.7.0_45/?/usr/local/jdk vim?/etc/profile exportJAVA_HOME=/usr/local/jdk exportJRE_HOME=/usr/local/jdk/jre exportPATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH exportCLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/libupdate-alternatives?--install?/usr/bin/java?java?/usr/local/jdk/bin/java300 update-alternatives?--install?/usr/bin/javac?javac/usr/local/jdk/bin/javac?300 update-alternatives?--config?java update-alternatives?--config?javacjava?–version4.2 安裝tomcat
從以下的地址下載tomcat組件(http://tomcat.apache.org/download-70.cgi),選擇core版本就可以了。
wget?http://apache.dataguru.cn/tomcat/tomcat-7/v7.0.47/bin/apache-tomcat-7.0.47.tar.gz tar?zxvf?apache-tomcat-7.0.47.tar.gz mv?apache-tomcat-7.0.47?/usr/local/tomcat /usr/local/tomcat/bin/startup.sh ps?aux?|grep?tomcat netstat?–tlnpvim?/usr/local/tomcat/conf/server.xml <Connectorport="8080"?protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443"/><Host?name="localhost"??appBase="/var/www/html"unpackWARs="true"autoDeploy="true">??#根目錄,頁面文件要放在/var/www/html/ROOT下面mkdir?/var/www/html/ROOT vim?/var/www/html/ROOT/index.jsp Hello,tomcat?home!vim?/usr/local/tomcat/conf/web.xml4.3 設置nginx
可以通過代理轉發的方式實現和tomcat的整合,通過設定只要擴展名為.jsp的請求就轉發至8080的tomcat進行處理。
vim?/etc/nginx/proxy.conf proxy_redirect?????????off; proxy_set_header????????Host?$host; proxy_set_header????????X-Real-IP$remote_addr;?#獲取真實IP #proxy_set_header??????X-Forwarded-For???$proxy_add_x_forwarded_for;?#獲取代理者的真實ip 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???????????432k; proxy_busy_buffers_size?64k; proxy_temp_file_write_size?64k; vim?/etc/nginx/nginx.conf location?~?\*.jsp$?{root?./ROOT;index?index.jsp;proxy_pass??????http://127.0.0.1:8080;}4.4 測試
先重啟tcomcat和nginx并新建html測試文件,如下所示。
vim?/var/www/html/index.html the?port:80kill?`ps?aux?|grep?tomcat?|awk?'/[0-9]/{print?$2}'?|?head?-n1` /usr/local/tomcat/bin/startup.sh service?nginx?restart五、訪問者地理信息記錄
Nginx可以通過配置使用http_geoip_module模塊來記錄、使用訪問者的信息,或是根據這些信息有選擇的提供服務。
5.1 配置地理數據文件
wget?http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz tar?zxvf?GeoIP.tar.gz cd?GeoIP-1.4.8/ ./configure make make?install如果在編譯時出現“Libtool library used but `LIBTOOL' is undefined”的錯誤提示,這是因為?libGeoIP?自帶了一個很舊的?ltmain.sh,這個文件導致成的?libtool?也很舊,這 個?libtool?忽略了在?link?時調用它時傳給它的?-arch?參數,導致生成的?.dylib?不是?UB?的,從而導致最后 的?link?失敗。解決方法如下:
yum?install?libtool aclocal libtoolize?–forcehttp_geoip_module模塊會創建一些ngx_http_geoip_module變量,這些編碼是基于客戶端IP的,它會與MaxMind GeoIP文件進行匹配查詢。默認情況下這些文件需要另外下載安裝。
wget?http://geolite.maxminx.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz wget?http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gzip?-d?GeoIP.dat.gz gzip?-d?GeoLiteCity.dat.gz mkdir?/usr/local/nginx/geoip mv?/root/*.dat?./5.2 配置nginx模塊
Nginx的默認安裝并不包括http_geoip_module模塊,需要在安裝nginx時指定—with-http-geoip_module選項。下圖是未加載該模塊的情況:
可以通下以下的指令新增模塊,和全新安裝nginx的方式相比,不需要執行make install只需在make成功后將nginx執行文件復制到原始的安裝路徑下就可以了。成功后的結果如下圖所示:
./configure--prefix=/usr/local/nginx? --conf-path=/etc/nginx/nginx.conf--pid-path=/var/run/nginx/nginx.pid? --error-log-path=/var/log/nginx/nginx.log--http-log-path=/var/log/nginx/nginx-http.log--with-http_geoip_module make mv?/usr/local/nginx/sbin/nginx?/usr/local/nginx/sbin/nginx.orig cp?/root/tengine/obj/nginx?/usr/local/nginx/sbin/vim?/etc/nginx/nginx.conf geoip_country/usr/local/nginx/geoip/GeoIP.dat; geoip_city/usr/local/nginx/geoip/GeoLiteCity.dat;fastcgi_param???GEOIP_COUNTRY_CODE3?$geoip_country_code3; fastcgi_param???GEOIP_COUNTRY_NAME?$geoip_country_name; fastcgi_param???GEOIP_CITY_COUNTRY_CODE$geoip_city_country_code; fastcgi_param???GEOIP_CITY_COUNTRY_CODE3$geoip_city_country_code3; fastcgi_param???GEOIP_CITY_COUNTRY_NAME$geoip_city_country_name; fastcgi_param???GEOIP_REGION?$geoip_region; fastcgi_param???GEOIP_CITY?$geoip_city; fastcgi_param???GEOIP_POSTAL_CODE$geoip_city_continent_code; fastcgi_param???GEOIP_LATITUDE?$geoip_latitude; fastcgi_param???GEOIP_LONGITUDE?$geoip_longitude; include?????????fastcgi_params;/usr/local/nginx/sbin/nginx?–s?reload5.3 測試地理信息
建立以下的php測試文件檢測IP所在的地區
vim?/var/www/html/geoip.php <html> <head> <title>What?is?my?IPaddress?-?determine?or?retrieve?my?IP?address?</title> </head> <body> <?php if(getenv(HTTP_X_FORWARDED_FOR)){ $pipaddress?=?getenv(HTTP_X_FORWARDED_FOR); $ipaddress?=getenv(REMOTE_ADDR); echo?"You?Proxy?IPaddress?is?:".$pipaddress."(via?$ipaddress)";}?else?{$ipaddress?=getenv(REMOTE_ADDR);echo?"YourIP?address?is?:$ipaddress";} $country?=getenv(GEOIP_COUNTRY_NAME); $country_code?=getenv(GEOIP_COUNTRY_CODE); echo?"<br/>Yourcountry?:$country($country_code)"; ?> </body> </html>vim?/var/www/html/geoip_city.php <html> <head> <title>What?is?my?IPaddress?-?determine?or?retrieve?my?IP?address</title> </head> <body> <?php if?(getenv(HTTP_X_FORWARDED_FOR))?{$pipaddress?=?getenv(HTTP_X_FORWARDED_FOR);$ipaddress?=?getenv(REMOTE_ADDR);echo"<br>Your?Proxy?IP?address?is:".$pipaddress."(via?$ipaddress)"; }?else?{$ipaddress?=?getenv?(REMOTE_ADDR);echo"<br>Your?IP?address?is:?$ipaddress";} $geoip_city_country_code?=getenv(GEOIP_CITY_COUNTRY_CODE); $geoip_city_country_code3?=getenv(GEOIP_CITY_COUNTRY_CODE3); $geoip_city_country_name?=getenv(GEOIP_CITY_COUNTRY_NAME); $geoip_region?=getenv(GEOIP_REGION); $geoip_city?=getenv(GEOIP_CITY); $geoip_postal_code?=getenv(GEOIP_POSTAL_CODE); $geoip_city_continent_code=?getenv(GEOIP_CITY_CONTTINENT_CODE); $geoip_latitude?=getenv(GEOIP_LATITUDE); $geoip_longitude?=getenv(GEOIP_LONGITUDE); echo"<br>Country:$geoip_city_country_name($geoip_city_country_code3,$geoip_city_country_code)"; echo"<br>Region:$geoip_region"; echo"<br>City:$geoip_city"; echo"<br>Postalcode:$geoip_postal_code"; echo"<br>Citycontinent?code:$geoip_city_continent_code"; echo"<br>Geoiplatitude:$geoip_latitude"; echo"<br>Geoiplongitude:$geoip_longitude"; ?> </body>總結
以上是生活随笔為你收集整理的nginx操作指南之二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rtsp交互命令简介及过程参数描述
- 下一篇: Faster\Slower 快慢指针的应