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

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。

 

 

目录
相关文章
|
网络协议 应用服务中间件 网络安全
Nginx,正向代理
本文介绍了Nginx作为HTTPS正向代理的两种方案:HTTP CONNECT隧道(7层)和NGINX stream(4层)。HTTP CONNECT隧道需要客户端手动配置代理,通过CONNECT请求建立隧道;而NGINX stream则更适合透明代理,利用SNI字段实现流量转发。文章详细讲解了两者的原理、环境搭建、使用场景及常见问题,并提供了配置示例和最佳实践建议。内容转载自阿里云开发者社区@怀知的文章,推荐读者参阅原文获取更多信息。感谢您的阅读!
3566 80
Nginx,正向代理
|
存储 安全 应用服务中间件
将下载的Nginx证书转换为Tomcat证书格式
好,可以看到,将Nginx证书转换为Tomcat证书的过程就像在烘焙一块蛋糕。你需要准备材料(证书),配备工具(OpenSSL, keytool),按照一定的步骤慎重制作,最后你就拥有了一块可以在浏览器中呈现出漂亮的安全状态的HTTPS蛋糕。就这么简单,明了,没有任何复杂的理论知识,就像在家庭厨房里烘焙的快乐一样。
570 16
|
负载均衡 Java 应用服务中间件
Tomcat与Nginx的负载均衡与动静分离技巧
总的来说,Tomcat和Nginx各有各的优点,在负载均衡和动静分离这两方面它们都有很好的应用。灵活使用这两个工具能够让Web应用具有更好的扩展性和用户体验。
460 14
|
负载均衡 前端开发 JavaScript
LVS-DR模式、keepalived、Nginx与Tomcat合作,打造动静分离,高效负载均衡与高可用性
为了采用这样的架构,你需要对LVS-DR、Keepalived、Nginx与Tomcat有一定的理解和掌握,同时也需要投入一些时间去研究和配置,但是一旦你把它运行起来,你将会发现,这一切都是值得的。
567 11
|
监控 应用服务中间件 测试技术
确保正则表达式在 Nginx 代理中的准确性和稳定性
【10月更文挑战第19天】总之,正则表达式在 Nginx 代理中具有重要作用,但要确保其准确性和稳定性需要付出一定的努力和关注。通过以上方法的综合运用,我们可以提高正则表达式配置的可靠性,为用户提供更好的服务体验。
|
应用服务中间件 API nginx
使用正则表达式实现 Nginx 代理
【10月更文挑战第19天】在不断发展的互联网技术中,掌握正则表达式在 Nginx 代理中的应用是非常重要的。不断探索和实践,将有助于我们在实际工作中更好地运用这一技术,提升项目的质量和效率。
|
缓存 负载均衡 应用服务中间件
Nginx 实现一个端口代理多个前后端服务
【10月更文挑战第19天】Nginx 的强大功能不仅限于此,它还可以与其他技术和工具相结合,为我们的应用提供更强大的支持和保障。在不断发展的互联网时代,掌握 Nginx 的使用技巧将为我们的工作和生活带来更多的便利和效益。
|
前端开发 应用服务中间件 定位技术
Nginx 如何代理转发传递真实 ip 地址?
【10月更文挑战第32天】
4326 5
Nginx 如何代理转发传递真实 ip 地址?
|
缓存 Java 应用服务中间件
nginx的正向代理和反向代理以及tomcat
Nginx的正向代理和反向代理功能在不同的场景中具有重要作用,正向代理主要用于客户端访问控制和匿名浏览,而反向代理则用于负载均衡和高可用性服务。Tomcat作为Java Web应用服务器,与Nginx结合使用,可以显著提升Web应用的性能和稳定性。通过合理配置Nginx和Tomcat,可以构建高效、稳定和可扩展的Web服务架构。
679 11
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
906 5
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全