Nginx安装,Nginx静态缓存,Nginx Gzip压缩,Nginx负载均衡,Nginx方向代理,Nginx+Tomcat+Redis做session共享...
Nginx安裝
nginx-1.10.1.tar.gz安裝,參考http://blog.csdn.net/tototuzuoquan/article/details/47381907。
修改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 { ??? worker_connections? 1024; } ? 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? /usr/local/nginx/logs/access.log?? main; ??? ??? charset utf-8; ? ??? server_names_hash_bucket_size 128; ??? client_header_buffer_size 64k; ??? large_client_header_buffers 4 64k; ??? client_max_body_size 200m; ? ??? ##cache###### ??? proxy_connect_timeout 5; ??? proxy_read_timeout 60; ??? proxy_send_timeout 5; ??? proxy_buffer_size 16k; ??? proxy_buffers 4 64k; ??? proxy_busy_buffers_size 128k; ??? proxy_temp_file_write_size 128k; ##設(shè)置臨時(shí)目錄,其中/data/tpl_nginx_cache_dir/temp和/data/tpl_nginx_cache_dir/cache在同級(jí)目錄下,若這個(gè)目錄沒(méi)有,請(qǐng)自行創(chuàng)建(目錄名稱啥的沒(méi)有要求限制),這里我創(chuàng)建在了/data下面。 proxy_temp_path /data/tpl_nginx_cache_dir/temp; ??? ##設(shè)置緩存目錄為二級(jí)目錄,共享內(nèi)存區(qū)大小,非活動(dòng)時(shí)間,最大容量,注意臨時(shí)目錄要跟緩存目錄在同一個(gè)分區(qū) ??? proxy_cache_path /data/tpl_nginx_cache_dir/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; ??? ##cache###### ? ??? sendfile??????? on; ??? #tcp_nopush???? on; ? ??? #keepalive_timeout? 0; ??? keepalive_timeout? 65; ? ??? #開(kāi)啟Gzip壓縮 ??? gzip? on; ??? #不壓縮臨界值,大于1k的才壓縮,一般不用改 ??? gzip_min_length 1k; ??? #buffer相關(guān)設(shè)置 ??? gzip_buffers 4 16; ??? #用了反向代理的話,末端通信是HTTP/1.0,默認(rèn)是Http/1.1 ? ??#gzip_http_version 1.0; ??? #壓縮級(jí)別,1~10,數(shù)字越大壓縮的越好,時(shí)間也越長(zhǎng) ??? gzip_comp_level 3; ??? #進(jìn)行壓縮的文件類型,缺啥補(bǔ)啥就行了,JavaScript有兩種寫(xiě)法,最好都寫(xiě)上吧,總有人抱怨js文件沒(méi)有壓縮,其實(shí)多寫(xiě)一種格式就行了 ??? gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; ??? #跟Squid等緩存服務(wù)有關(guān),on的話會(huì)在Header里增加"Vary: Accept-Encoding",我不需要這玩意,自己對(duì)照情況看著辦吧 ??? gzip_vary off; ??? #IE6對(duì)Gzip不怎么友好,不給它Gzip了 ??? gzip_disable "MSIE [1-6]\."; ?????? ??? #下面的ip表示的是部署不同tomcat的ip和tomcat的端口 ?????? upstream LoadBalanceMachine { ?????? ??? server 192.168.1.249:8080 weight=1; ?????? ??? server 192.168.1.249:9002 weight=1; ?????? } ?????? ??? #下面的意思是監(jiān)聽(tīng)192.168.1.249服務(wù)器上的80端口。 ?????? server { ?????? ??? listen 80; ????????????? server_name 192.168.1.249; ????????????? charset utf-8; ????????????? ????????????? location / { ????????????? ??? #root html; ???????????????????? #index index.html index.htm; ???????????????????? proxy_pass http://LoadBalanceMachine; ???????????????????? client_max_body_size? 200m; ??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時(shí)候被解析到實(shí)際的ip ???????????????????? proxy_set_header??????? Host $host; ??????????? proxy_set_header??????? X-Real-IP $remote_addr; ??????????? proxy_set_header??????? X-Forwarded-For $proxy_add_x_forwarded_for; ????????????? } ????????????? ??????? #下面這個(gè)是配置云專題門(mén)戶的,對(duì)于本地部署版本的專題,可以不配置這個(gè)內(nèi)容 ????????????? location ^~ /project1{ ??????????? proxy_pass http://LoadBalanceMachine; #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時(shí)候被解析到實(shí)際的ip ???????????????????? proxy_set_header??????? Host $host; ??????????? proxy_set_header??????? X-Real-IP $remote_addr; ??????????? proxy_set_header??????? X-Forwarded-For $proxy_add_x_forwarded_for; ??????? } ????????????? ??????? #下面這個(gè)是配置云專題門(mén)戶的,對(duì)于本地部署版本的專題,可以不配置這個(gè)內(nèi)容 ????????????? location ~ .*/project1/ { ??????????? rewrite ^/(.*)/project1/(.*)$ http://LoadBalanceMachine/project1/$2; ??? ?????? } ????????????? ??????? #下面的意思表示的意思是部署專題project2。這個(gè)名稱是自己部署的專題的 ????????????? location ^~ /project2 { ??????????? proxy_pass http://LoadBalanceMachine; ??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時(shí)候被解析到實(shí)際的ip ???????????????????? proxy_set_header??????? Host $host; ??????????? proxy_set_header??????? X-Real-IP $remote_addr; ??????????? proxy_set_header??????? X-Forwarded-For $proxy_add_x_forwarded_for; ????????????? } ? ??????? #這個(gè)必須要寫(xiě) ????????????? location ~ .*/project2/ { ???????????????????? rewrite ^/(.*)/project2/(.*)$ http://LoadBalanceMachine/project2/$2; ????????????? } ? ??????? #下面的配置是對(duì)靜態(tài)資源做nginx靜態(tài)緩存的過(guò)程。 ????????????? location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) { ????????????? ??? proxy_pass http://LoadBalanceMachine; ????????????? ??? proxy_redirect off; ??????????? proxy_set_header Host $host; ????????????? ??? ##設(shè)置緩存共享區(qū)塊,也就是keys_zone名稱。 ??????????? proxy_cache cache_one; ????????????? ??? ##設(shè)置http狀態(tài)碼為200,302緩存時(shí)間為1小時(shí)。 ??????????? proxy_cache_valid 200 302 1h; ??????????? proxy_cache_valid 301 1d; ??????????? proxy_cache_valid any 1m; ????????????? ??? ##設(shè)置過(guò)期時(shí)間,為30天 ??????????? expires 30d; ????????????? } ????????????? ??????? #配置靜態(tài)緩存的時(shí)候,下面的必須配置的,當(dāng)訪問(wèn).action結(jié)尾的內(nèi)容的時(shí)候,訪問(wèn)到實(shí)際位置的action ????????????? location ~ .*\.(action)(.*) { ????????????? ??? proxy_pass http://LoadBalanceMachine; ??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時(shí)候被解析到實(shí)際的ip ????????????? ??? proxy_set_header??????? Host $host; ??????????? proxy_set_header??????? X-Real-IP $remote_addr; ??????????? proxy_set_header??????? X-Forwarded-For $proxy_add_x_forwarded_for; ????????????? } ????????????? ?????? ?#配置靜態(tài)靜態(tài)緩存的時(shí)候,下面的必須配置的,當(dāng)訪問(wèn).action結(jié)尾的內(nèi)容的時(shí)候,訪問(wèn)到實(shí)際位置的action ????????????? location ~ .*\.(jsp)(.*) { ????????????? ??? proxy_pass http://LoadBalanceMachine; ??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時(shí)候被解析到實(shí)際的ip ????????????? ??? proxy_set_header??????? Host $host; ??????????? proxy_set_header??????? X-Real-IP $remote_addr; ??????????? proxy_set_header??????? X-Forwarded-For $proxy_add_x_forwarded_for; ????????????? } ?????? } ?????? } |
?
Redis安裝
redis-3.2.6.tar.gz安裝,參考方式:
| 用源碼工程來(lái)編譯安裝 1、? 到官網(wǎng)下載最新stable版,這里使用的是:redis-3.2.6.tar.gz 2、? cd /usr/local ? 3、? make redis-src 4、? tar -zxvf ? ?redis-3.2.6.tar.gz? -C? ./redis-src/ 2、解壓源碼并進(jìn)入目錄cd ?/usr/local/redis-src/redis-3.2.6 3、?先執(zhí)行make,檢查是否報(bào)錯(cuò) 如果報(bào)錯(cuò)提示缺少gcc,則安裝gcc :? yum install -y gcc 如果報(bào)錯(cuò)提示:Newer version ofjemalloc required 則在make時(shí)加參數(shù):make MALLOC=libc (如果沒(méi)有報(bào)錯(cuò),直接使用make命令) ? 4、安裝redis,指定安裝目錄,如 /usr/local/redis make PREFIX=/usr/local/redis install ? 5、拷貝一份配置文件到安裝目錄下 切換到源碼目錄,里面有一份配置文件redis.conf,然后將其拷貝到安裝路徑下 cp redis.conf /usr/local/redis/ ? 6、啟動(dòng)redis cd /usr/local/redis bin/redis-server redis.conf ? (如果想后臺(tái)進(jìn)程運(yùn)行,修改:daemonize yes) ? vim redis.conf 將下面的變量修改為: daemonize yes ? 將bind的id換成真實(shí)的ip地址,比如: bind 192.168.1.1 ? 在集群配置中,要對(duì)redis配置密碼,修改的配置是將redis.conf這個(gè)文件的下面的變量配置成如下的: requirepass cloudTplWebapp??? (這里設(shè)置一個(gè)密碼:cloudTplWebapp) 7、連接redis 另開(kāi)一個(gè)xshell,然后: #cd /usr/local/redis/ [root@hadoop redis]# bin/redis-cli? 127.0.0.1:6379> |
?
tomcat-redis-session-manager開(kāi)源項(xiàng)目的使用
1、開(kāi)源項(xiàng)目地址:https://github.com/jcoleman/tomcat-redis-session-manager
2、下載代碼之后需要進(jìn)行重新編譯,生成所需要的jar,任意創(chuàng)建maven項(xiàng)目,將src下的代碼拷貝到具體位置,如下:
maven的pom.xml文件如下:
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" ???????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ???????? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> ??? <modelVersion>4.0.0</modelVersion> ? ??? <groupId>com.ufind.session</groupId> ??? <artifactId>tomcat-redis-session</artifactId> ??? <version>1.0-SNAPSHOT</version> ? ??? <dependencies> ??????? <dependency> ??????????? <groupId>org.apache.tomcat</groupId> ??????????? <artifactId>tomcat-catalina</artifactId> ??????????? <version>7.0.27</version> ??????? </dependency> ??????? <dependency> ??????????? <groupId>redis.clients</groupId> ??????????? <artifactId>jedis</artifactId> ??????????? <version>2.7.2</version> ??????? </dependency> ??? </dependencies> ? ??? <build> ??????? <plugins> ??????????? <plugin> ??????????????? <groupId>org.apache.maven.plugins</groupId> ??????????????? <artifactId>maven-compiler-plugin</artifactId> ??????????????? <version>3.0</version> ??????????? ????<configuration> ??????????????????? <source>1.7</source> ??????????????????? <target>1.7</target> ??????????????????? <encoding>UTF-8</encoding> ??????????????? </configuration> ??????????? </plugin> ??????? </plugins> ??? </build> ? </project> |
?
3、然后打開(kāi)terminal,執(zhí)行mvn clean 和mvn install 將編譯好的代碼打包為:tomcat-redis-session-1.0-SNAPSHOT.jar
4、將tomcat-redis-session-1.0-SNAPSHOT.jar、jedis-2.7.3.jar、commons-pool2-2.3.jar三個(gè)jar包分別放在tomcat1和tomcat2實(shí)例下的lib目錄下。
圖 32 jar包所在位置
5、修改tomcat實(shí)例下conf/contex.xml文件
| <?xml version='1.0' encoding='utf-8'?> <!-- ? Licensed to the Apache Software Foundation (ASF) under one or more ? contributor license agreements.? See the NOTICE file distributed with ? this work for additional information regarding copyright ownership. ? The ASF licenses this file to You under the Apache License, Version 2.0 ? (the "License"); you may not use this file except in compliance with ? the License.? You may obtain a copy of the License at ? ????? http://www.apache.org/licenses/LICENSE-2.0 ? ? Unless required by applicable law or agreed to in writing, software ? distributed under the License is distributed on an "AS IS" BASIS, ? WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ? See the License for the specific language governing permissions and ? limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> ? ??? <!-- Default set of monitored resources --> ??? <WatchedResource>WEB-INF/web.xml</WatchedResource> ? ??? <!-- Uncomment this to disable session persistence across Tomcat restarts --> ??? <!-- ??? <Manager pathname="" /> ??? --> ? ??? <!-- Uncomment this to enable Comet connection tacking (provides events ???????? on session expiration as well as webapp lifecycle) --> ??? <!-- ??? <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> ??? --> ?? <!— tomcat-redis-session共享配置,下面的host表示的是redis的ip地址。 port:表示的意思是redis的端口。 password:表示的意思是redis的password (這里就是5.2.2中Redis配置的password的值,如果不配置密碼 database:選擇的redis的db是0) --> ??? <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> ?????? <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" ???????????????????? ?host="192.168.1.1" ???????????????????? ?port="6379" ???????????????????? ?database="0" ???????????????????? ?password="cloudTplWebapp" ???????????????????? ?maxInactiveInterval="60" /> </Context> |
分別修改:/data/tomcat1/bin和? /data/tomcat2/bin 下的catalina.sh,修改JVM參數(shù)信息和指定JDK
| export "JAVA_OPTS=-Xms512m -Xmx1024m -XX:PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=1024m" export "JAVA_HOME=/data/jdk8-for-tomcat7/jdk1.8.0_121" |
?
最后,重啟一下tomcat。
?
?
總結(jié)
以上是生活随笔為你收集整理的Nginx安装,Nginx静态缓存,Nginx Gzip压缩,Nginx负载均衡,Nginx方向代理,Nginx+Tomcat+Redis做session共享...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Ubuntu 常用工具、指令安装
- 下一篇: JavaScript的理解记录(6)