`
thrillerzw
  • 浏览: 139129 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

nginx

 
阅读更多

实现nginx的安装,负载到2台tomcat,解决session一致的问题。

 

一:centos5安装nginx

1 在安装之前首先要安装pcre-devel openssl openssl-devel
 yum -y install pcre-devel openssl openssl-devel 
或者源码安装,如:
# tar zxvf pcre-8.01.tar.gz
# cd  pcre-8.01
# ./configure
# make && make install
否则:
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.

说明:PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库。

2 安装nginx :

seesion共享:下载补丁:http://code.google.com/p/nginx-upstream-jvm-route/  nginx0.8不好用。估计高版本都不好用?

#安装svn客户端
[root@Slave1 nginx-0.7.69]# yum -y install subversion
# svn checkout http://nginx-upstream-jvm-route.googlecode.com/svn/trunk/ nginx-upstream-jvm-route-read-only
# tar zxvf nginx-0.7.69.tar.gz
# cd nginx-0.7.69
[root@Slave1 nginx-0.7.69]# patch -p0 <  /opt/nginx/nginx-upstream-jvm-route-read-only/jvm_route.patch

#出现以下信息说明正确

patching file src/http/ngx_http_upstream.c
Hunk #1 succeeded at 3861 (offset 19 lines).
Hunk #3 succeeded at 3993 (offset 19 lines).
Hunk #5 succeeded at 4063 (offset 19 lines).
patching file src/http/ngx_http_upstream.h

 

# ./configure --user=root --group=root --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module  --add-module=/opt/nginx/nginx-upstream-jvm-route-read-only
# make && make install

检查nginx的配置文件

# /usr/local/webserver/nginx/sbin/nginx -t

启动nginx

# /usr/local/webserver/nginx/sbin/nginx

 

测试安装成功:http://192.168.1.101/ 显示: Welcome to nginx!

重启nginx

# /usr/local/webserver/nginx/sbin/nginx -s reload

 

nginx -s stop     // 停止nginx

nginx -s reload   // 重新加载配置文件

nginx -s quit     // 退出nginx

 

windows下面启动nginx

cd nginx目录:

start nginx  简单点直接双击nginx.exe

打开任务管理器,查看 nginx.exe 进程,有二个进程会显示,占用系统资源,那是相当的少。

 

配置nginx开机启动

编辑/etc/rc.d/rc.local 文件,在末尾加入:/usr/local/webserver/nginx/sbin/nginx 重启则可以开机启动

 

补充:

在Nginx 中,如果服务器提供安全网页时则会用到OpenSSL库,我们需要安装库文件和它的开发安装包
OpenSSL项目是一个协作开发健壮的、商业级的、全功能的、开源工具执行于安全套接层(SSL v2/v3)和传输层安全(TLS v1)的协议,也是一个完整强壮的通用加密库。

 

二:反向代理:

反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

三:负载均衡:

#nginx所用用户和组   
user  root root; 

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。   
    use epoll;   
       
    #允许最大连接数   
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
		
		upstream localhost {   
      #ip_hash   
      #ip_hash;   
        server localhost:8080 srun_id=jvm1;   
        server localhost:8180 srun_id=jvm2;
        jvm_route $cookie_JSESSIONID|sessionid reverse;   
     }   


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
              proxy_connect_timeout   3;   
              proxy_send_timeout      30;   
              proxy_read_timeout      30;   
              proxy_pass http://localhost;   
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

tomcat:
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

 同一台机器启动2个tomcat需要修改一个:<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
 否则启动报错:严重: Can't find free port 8009 8009

 

四、rewrite

 

通过rewrite规则,可以实现规范的URL、根据变量来做URL转向及选择配置,一些动态URL地址须要伪装成静态HTML,便于搜索引擎抓取,也需要rewrite来处理。一些由于目录结构、域名变化的旧url,需要跳转到新的url上。

flag标记有:

* last 相当于Apache里的[L]标记,表示完成rewrite

* break 本条匹配完成后,终止匹配, 不再匹配后面的规则

* redirect 返回302临时重定向 地址栏会显示跳转后的地址

* permanent 返回301永久重定向 地址栏会显示跳转后的地址

last 与 break 是有区别的,last标记 在匹配后继续搜索,而break 标记在匹配后则停止搜索。

因此,一般在根location中 (location /),或者直接写在server 标签中的rewrite 规则请使用last标记,而在非根location中 (location /bbs/)请使用break标记。

/photos/123456  -> /path/to/photos/12/1234/123456.png

#带{},使用双引号/单引号 包围。

rewrite  "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;

 

server {
        listen       80;
        server_name  xx.xx.com;
        root  /usr/local/www/html/xx;
        access_log  logs/xx.access.log; # main;
  		error_page 405 =200 $request_uri;
        location / {
            index  index.html index.htm;
            rewrite ^/cms/js/(.*)$ /js/$1 last;
            rewrite ^/cms/assets/(.*)$ /assets/$1 last;
        }
        
        location ^~ /cms/upload/ {
            root /usr/local/www/html;
        }
        location  ~ ^/info/([0-9]+)\-([0-9]+)\.html$ {
            rewrite "^/info/([0-9]+)\-([0-9]+)\.html$"   /cms/information_list.do?siteid=5&cateid=$1&page.page=$2 last;
        }
}

 

 

五、例子说明

    #负载 upstream的分配方式。http://onlyzq.blog.51cto.com/1228/557848
	 upstream usdemo{
     		server 192.168.1.10;
     		server 192.168.1.11;
    	}
    #虚拟主机,反向代理usdemo组主机。可以有多个虚拟主机
    server {
        listen       80 ;
        server_name  xx.xx.com;
        access_log  logs/xx.access.log  main;
        error_log  logs/xx.error.log  error;
   
         location ^~ /message/ {
          proxy_intercept_errors on;
          proxy_pass http://xx;
          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          break;
        }
       #访问除了/message/之外的所有地址
        location / {
            root   html;
            index   index.shtml index.html;
	    proxy_intercept_errors on;
	    #跳转到
            proxy_pass http://usdemo;
	    #设置头信息
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            break;
        } 
    }

 

nginx虚拟目录(alias与root的区别) : http://blog.sina.com.cn/s/blog_6c2e6f1f0100l92h.html
一般情况下,在location /中配置root,在location /other中配置alias是一个好习惯

 

访问本地磁盘图片

     location ^~ /cms/upload/ {
              #http://www.dhresource.com/cms/upload/mImg/2014/02/11/89e4934b68a44a7f9c92baa7484c46b3.jpg 改成root不行
            alias E:/xx/resource/;
    }
    或者
    location / {
         #http://www.dhresource.com/cms/upload/mImg/2014/02/11/89e4934b68a44a7f9c92baa7484c46b3.jpg   root:静态文件存放的目录
            root E:/xx/resource/;
    }

 

F5是操作于IOS网络模型的传输层,Nginx、apache是基于http反向代理方式,位于ISO模型的第七层应用层。直白些就是TCP UDP 和http协议的区别,Nginx不能为基于TCP协议的应用提供负载均衡。

 

分享到:
评论

相关推荐

    一分钟搞定 Nginx反向代理 nginx域名代理

    3.找到D:\nginx\conf下nginx.conf文件用记事本打开 在文段末尾大括号前加上 include proxy.conf;(就是加载刚刚新建的那个文件(注意路径)) 4.进入cmd 进入D盘: d: 进到nginx文件夹下:cd nginx 启动nginx.exe:...

    nginx-upstream-jvm-route 和 nginx 对应版本,亲测可用

    此资源有两个文件,含 nginx-upstream-jvm-route 和 nginx 对应版本,都是tar.gz文件。 安装方法网上很多就不写了,亲测可用。 不用担心版本不匹配造成安装失败,再浪费积分去到处下载尝试的烦恼。 此资源有两个文件...

    Nginx 1.22.0 Linux 版本,解压安装。

    Nginx 1.22.0 Linux 版本,解压安装。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型...

    nginx-1.23.1.zip

    nginx/Windows-1.23.1 Nginx(发音为“engine X”[9] /ˌɛndʒɪnˈɛks/ EN-jin-EKS),风格化为NGIИX,是一个Web服务器,也可以用作反向代理,负载平衡器,邮件代理和HTTP缓存。该软件由Igor Sysoev创建,并于...

    Nginx 全能HTTP+Web 指南 完整版pdf

    Nginx全能指南是一本介绍Nginx服务器的书,首先,简要介绍Nginx的基本概念和作用,如反向代理、负载均衡等。然后,列举Nginx的优点,如高性能、可扩展性、稳定性等。接着,介绍如何安装和配置Nginx,并提供一些实用...

    使用nginx部署前端项目(超详细教程).pdf

    使用nginx部署前端项目是一篇非常详细的教程,旨在帮助初学者使用Nginx来部署前端项目。本文首先介绍了Nginx的基本概念和作用,解释了为什么Nginx是一个强大的Web服务器和反向代理。然后,文章详细讲解了如何在Linux...

    nginx离线安装依赖项-linux系统/麒麟v10系统

    nginx离线安装依赖项_linux系统/麒麟v10系统. 离线安装 内网离线安装Nginx 安装Nginx的依赖包如下: PCRE(Perl Compatible Regular Expressions):Nginx使用PCRE来支持正则表达式,可以使用它更灵活地匹配和处理...

    Nginx服务器的安装与配置.pdf

    Nginx官网配置.pdf Nginx基本配置.pdf Nginx模块.pdf Nginx指南.pdf 第1章 Nginx简介.pdf 第2章 Nginx服务器的安装与配置.pdf 第3章 Nginx的基本配置与优化.pdf 第4章 Nginx与PHP(FastCGI)的安装、配置与优化.pdf 第...

    nginx-1.19.3-http-flv.zip

    资源说明: 1. 采用nginx最新版编译,包含最新的nginx-http-flv-module,以及基础模块openssl、prce、zlib 2. 整体打包,已配置好nginx.conf的http-flv直播流,以及http web环境...3. 使用nginxservice.exe install安装

    nginx常用依赖包.rar

    本压缩包均为linux 64位系统的,其中包含nginx-1.14.2.tar.gz、nginx-1.15.4.tar.gz、nginx-1.16.1.tar.gz、nginx-1.17.5.tar.gz及pcre-8.41.tar.gz、pcre-8.42.tar.gz、pcre-8.43.tar.gz、pcre-8.44.tar.gz及...

    Nginx启动、重启失败的一般解决方法和步骤

    也可以通过查看Nginx日志文件定位到Nginx重启失败的原因,Nginx日志文件的路径一般在:/var/log/nginx目录下 总结 文章没什么技术含量,每天都攒一点基础知识 您可能感兴趣的文章:nginx centos 服务开机启动设置...

    linux服务器上使用nginx访问本地静态资源的方法

    vim /etc/nginx/conf.d/nginx.conf 5.重启nginx服务 service nginx restart 6.页面尝试访问本地资源 7.访问成功! 总结 以上所述是小编给大家介绍的linux服务器上使用nginx访问本地静态资源的方法,希望对大家...

    Nginx配置统计流量带宽请求及记录实时请求状态的方法

    ngx_req_status用来展示nginx请求状态信息,类似于apache的status,nginx自带的模块只能显示连接数等等信息,我们并不能知道到底有哪些请求、以及各url域名所消耗的带宽是多少。ngx_req_status提供了这些功能. 功能...

    nginx 离线安装包nginx 离线安装包

    nginx 离线安装包nginx 离线安装包

    nginx-sticky-module-1.25.zip

    nginx sticky是nginx的module,可以实现基于cookie的负载均衡。 下载后,在编译安装nginx时,用--add-module选项,指到sticky所在目录。类似命令如下: ./configure --prefix=/usr/local/nginx-1.6.0 --add-module=...

    nginx-1.18.0离线安装依赖包及过程

    nginx-1.18安装步骤 附件上传至服务器/opt/nginx cd /opt/nginx tar zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make make install tar zxvf pcre-8.40.tar.gz cd pcre-8.40 ./configure make make ...

    Nginx文件上传模块配置

    Nginx是一个高性能的HTTP和反向代理服务器,也是IMAP/POP3/SMTP代理服务器,是由俄罗斯人lgor Sysoev开发,支持模块加载和卸载,其中upload_module和upload_progress_module就是第三方开发的模块,并没有加入到Nginx...

    可自动按天分割日志的nginx (根据nginx源代码修改得来)

    将在 logs目录下生成类似 2014-01-21.access.log 的log文件,这个日期会根据服务器的时间变化) 配置可参照 nginx目录下的 conf/nginx.conf 文件进行log配置 源码留给有特殊需要的朋友自己编译。此源码就是修改后的...

    Nginx日志管理介绍

    Nginx日志描述 通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的...

Global site tag (gtag.js) - Google Analytics