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

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 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

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;

##设置临时目录,其中/data/tpl_nginx_cache_dir/temp/data/tpl_nginx_cache_dir/cache在同级目录下,若这个目录没有,请自行创建(目录名称啥的没有要求限制),这里我创建在了/data下面。

proxy_temp_path /data/tpl_nginx_cache_dir/temp;

    ##设置缓存目录为二级目录,共享内存区大小,非活动时间,最大容量,注意临时目录要跟缓存目录在同一个分区

    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相关设置

    gzip_buffers 4 16;

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

    #gzip_http_version 1.0;

    #压缩级别,1~10,数字越大压缩的越好,时间也越长

    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等缓存服务有关,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;

       }

      

    #下面的意思是监听192.168.1.249服务器上的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;

            #下面必须设置,下面的意思是让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 ^~ /project1{

            proxy_pass http://LoadBalanceMachine;

#下面必须设置,下面的意思是让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 ~ .*/project1/ {

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

           }

             

        #下面的意思表示的意思是部署专题project2。这个名称是自己部署的专题的

              location ^~ /project2 {

            proxy_pass http://LoadBalanceMachine;

            #下面必须设置,下面的意思是让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;

              }

 

        #下面的配置是对静态资源做nginx静态缓存的过程。

              location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {

                  proxy_pass http://LoadBalanceMachine;

                  proxy_redirect off;

            proxy_set_header Host $host;

                  ##设置缓存共享区块,也就是keys_zone名称。

            proxy_cache cache_one;

                  ##设置http状态码为200,302缓存时间为1小时。

            proxy_cache_valid 200 302 1h;

            proxy_cache_valid 301 1d;

            proxy_cache_valid any 1m;

                  ##设置过期时间,为30天

            expires 30d;

              }

             

        #配置静态缓存的时候,下面的必须配置的,当访问.action结尾的内容的时候,访问到实际位置的action

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

                  proxy_pass http://LoadBalanceMachine;

            #下面必须设置,下面的意思是让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;

              }

             

        #配置静态静态缓存的时候,下面的必须配置的,当访问.action结尾的内容的时候,访问到实际位置的action

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

                  proxy_pass http://LoadBalanceMachine;

            #下面必须设置,下面的意思是让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、  到官网下载最新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、 先执行make,检查是否报错

如果报错提示缺少gcc,则安装gcc :  yum install -y gcc

如果报错提示:Newer version ofjemalloc required

则在make时加参数: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    (这里设置一个密码: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,任意创建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,执行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参数信息和指定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。

 

 

目录
相关文章
|
3月前
|
负载均衡 应用服务中间件 nginx
百度搜索:蓝易云【Nginx和tomcat实现负载均衡教程】
至此,你已经成功地使用Nginx和Tomcat实现了负载均衡。Nginx将根据配置的负载均衡策略将客户端请求分发到多个Tomcat服务器上,以提高系统的性能和可用性。请注意,在实际生产环境中,还需要进行其他配置和优化,如健康检查、会话保持等,以满足具体的需求。
34 0
|
3月前
|
负载均衡 安全 前端开发
百度搜索:蓝易云【Nginx与Tomcat负载均衡-动静分离教程】
这些是将Nginx与Tomcat结合使用实现负载均衡和动静分离的基本步骤。根据您的需求和具体环境,可能还需要进行其他配置和调整。请确保在进行任何与网络连接和安全相关的操作之前,详细了解您的网络环境和安全需求,并采取适当的安全措施。
48 1
|
3月前
|
负载均衡 应用服务中间件 nginx
nginx-tomcat反向代理以及负载均衡测试
nginx-tomcat反向代理以及负载均衡测试
|
5月前
|
缓存 NoSQL fastjson
Shiro Session集群共享存入Redis中SimpleSession的transient 属性不能序列化
Shiro Session集群共享存入Redis中SimpleSession的transient 属性不能序列化
|
5月前
|
NoSQL Redis
shiro的session信息放redis反序列化异常解决
shiro的session信息放redis反序列化异常解决
|
1月前
|
存储 NoSQL 前端开发
【SpringBoot】Redis集中管理Session和自定义用户参数解决登录状态及校验问题
【SpringBoot】Redis集中管理Session和自定义用户参数解决登录状态及校验问题
|
1月前
|
缓存 NoSQL 安全
【Redis】2、Redis应用之【根据 Session 和 Redis 进行登录校验和发送短信验证码】
【Redis】2、Redis应用之【根据 Session 和 Redis 进行登录校验和发送短信验证码】
46 0
|
4月前
|
NoSQL 前端开发 中间件
黑马点评回顾 redis实现共享session
黑马点评回顾 redis实现共享session
33 0
|
4月前
|
负载均衡 安全 应用服务中间件
Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例
Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例
178 0
|
4月前
|
负载均衡 算法 应用服务中间件
Nginx+Tomcat实现反向代理与负载均衡入门
Nginx+Tomcat实现反向代理与负载均衡入门
180 0