日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Nginx安装,Nginx静态缓存,Nginx Gzip压缩,Nginx负载均衡,Nginx方向代理,Nginx+Tomcat+Redis做session共享...

發(fā)布時間:2025/3/17 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx安装,Nginx静态缓存,Nginx Gzip压缩,Nginx负载均衡,Nginx方向代理,Nginx+Tomcat+Redis做session共享... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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è)置臨時目錄,其中/data/tpl_nginx_cache_dir/temp/data/tpl_nginx_cache_dir/cache在同級目錄下,若這個目錄沒有,請自行創(chuàng)建(目錄名稱啥的沒有要求限制),這里我創(chuàng)建在了/data下面。

proxy_temp_path /data/tpl_nginx_cache_dir/temp;

??? ##設(shè)置緩存目錄為二級目錄,共享內(nèi)存區(qū)大小,非活動時間,最大容量,注意臨時目錄要跟緩存目錄在同一個分區(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;

?

??? #開啟Gzip壓縮

??? gzip? on;

??? #不壓縮臨界值,大于1k的才壓縮,一般不用改

??? gzip_min_length 1k;

??? #buffer相關(guān)設(shè)置

??? gzip_buffers 4 16;

??? #用了反向代理的話,末端通信是HTTP/1.0,默認是Http/1.1

? ??#gzip_http_version 1.0;

??? #壓縮級別,1~10,數(shù)字越大壓縮的越好,時間也越長

??? gzip_comp_level 3;

??? #進行壓縮的文件類型,缺啥補啥就行了,JavaScript有兩種寫法,最好都寫上吧,總有人抱怨js文件沒有壓縮,其實多寫一種格式就行了

??? 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的話會在Header里增加"Vary: Accept-Encoding",我不需要這玩意,自己對照情況看著辦吧

??? gzip_vary off;

??? #IE6對Gzip不怎么友好,不給它Gzip了

??? gzip_disable "MSIE [1-6]\.";

??????

??? #下面的ip表示的是部署不同tomcatiptomcat的端口

?????? upstream LoadBalanceMachine {

?????? ??? server 192.168.1.249:8080 weight=1;

?????? ??? server 192.168.1.249:9002 weight=1;

?????? }

??????

??? #下面的意思是監(jiān)聽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解析的時候被解析到實際的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;

????????????? }

?????????????

??????? #下面這個是配置云專題門戶的,對于本地部署版本的專題,可以不配置這個內(nèi)容

????????????? location ^~ /project1{

??????????? proxy_pass http://LoadBalanceMachine;

#下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時候被解析到實際的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;

??????? }

?????????????

??????? #下面這個是配置云專題門戶的,對于本地部署版本的專題,可以不配置這個內(nèi)容

????????????? location ~ .*/project1/ {

??????????? rewrite ^/(.*)/project1/(.*)$ http://LoadBalanceMachine/project1/$2;

??? ?????? }

?????????????

??????? #下面的意思表示的意思是部署專題project2。這個名稱是自己部署的專題的

????????????? location ^~ /project2 {

??????????? proxy_pass http://LoadBalanceMachine;

??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時候被解析到實際的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;

????????????? }

?

??????? #這個必須要寫

????????????? location ~ .*/project2/ {

???????????????????? rewrite ^/(.*)/project2/(.*)$ http://LoadBalanceMachine/project2/$2;

????????????? }

?

??????? #下面的配置是對靜態(tài)資源做nginx靜態(tài)緩存的過程。

????????????? 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緩存時間為1小時。

??????????? proxy_cache_valid 200 302 1h;

??????????? proxy_cache_valid 301 1d;

??????????? proxy_cache_valid any 1m;

????????????? ??? ##設(shè)置過期時間,為30天

??????????? expires 30d;

????????????? }

?????????????

??????? #配置靜態(tài)緩存的時候,下面的必須配置的,當(dāng)訪問.action結(jié)尾的內(nèi)容的時候,訪問到實際位置的action

????????????? location ~ .*\.(action)(.*) {

????????????? ??? proxy_pass http://LoadBalanceMachine;

??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時候被解析到實際的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)緩存的時候,下面的必須配置的,當(dāng)訪問.action結(jié)尾的內(nèi)容的時候,訪問到實際位置的action

????????????? location ~ .*\.(jsp)(.*) {

????????????? ??? proxy_pass http://LoadBalanceMachine;

??????????? #下面必須設(shè)置,下面的意思是讓LoadBalanceMachine解析的時候被解析到實際的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安裝,參考方式:

用源碼工程來編譯安裝

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、解壓源碼并進入目錄cd ?/usr/local/redis-src/redis-3.2.6

3、?先執(zhí)行make,檢查是否報錯

如果報錯提示缺少gcc,則安裝gcc :? yum install -y gcc

如果報錯提示:Newer version ofjemalloc required

則在make時加參數(shù):make MALLOC=libc (如果沒有報錯,直接使用make命令)

?

4、安裝redis,指定安裝目錄,如 /usr/local/redis

make PREFIX=/usr/local/redis install

?

5、拷貝一份配置文件到安裝目錄下

切換到源碼目錄,里面有一份配置文件redis.conf,然后將其拷貝到安裝路徑下

cp redis.conf /usr/local/redis/

?

6、啟動redis

cd /usr/local/redis

bin/redis-server redis.conf ? (如果想后臺進程運行,修改:daemonize yes)

?

vim redis.conf 將下面的變量修改為:

daemonize yes

?

將bind的id換成真實的ip地址,比如:

bind 192.168.1.1

?

在集群配置中,要對redis配置密碼,修改的配置是將redis.conf這個文件的下面的變量配置成如下的:

requirepass cloudTplWebapp??? (這里設(shè)置一個密碼:cloudTplWebapp)

7、連接redis

另開一個xshell,然后:

#cd /usr/local/redis/

[root@hadoop redis]# bin/redis-cli?

127.0.0.1:6379>

?

tomcat-redis-session-manager開源項目的使用

1、開源項目地址:https://github.com/jcoleman/tomcat-redis-session-manager

2、下載代碼之后需要進行重新編譯,生成所需要的jar,任意創(chuàng)建maven項目,將src下的代碼拷貝到具體位置,如下:


mavenpom.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、然后打開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三個jar包分別放在tomcat1和tomcat2實例下的lib目錄下。

圖 32 jar包所在位置

5、修改tomcat實例下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表示的是redisip地址。

port:表示的意思是redis的端口。

password:表示的意思是redispassword (這里就是5.2.2Redis配置的password的值,如果不配置密碼

database:選擇的redisdb0)

-->

??? <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共享...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。