nginx install

需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

5.安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx安装到 /usr/local/nginx 目录下的详细步骤:

cd /usr/local/

wget http://nginx.org/download/nginx-1.2.8.tar.gz

tar -zxvf nginx-1.2.8.tar.gz

cd nginx-1.2.8

./configure --prefix=/usr/local/nginx

make

make install

--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 (解压后的文件路径)的源码路径。

--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。

6.启动

确保系统的 80 端口没被其他程序占用,

/usr/local/nginx/sbin/nginx

检查是否启动成功:

netstat -ano|grep 80 有结果输入说明启动成功

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

7.重启

/usr/local/nginx/sbin/nginx –s reload

8.修改配置文件

cd /usr/local/nginx/conf

vi nginx.conf

9.常用配置

#nginx运行用户和组

user    www www;

#启动进程,通常设置成和cpu的数量相等

worker_processes  4;

#全局错误日志及PID文件

pid /var/run/nginx.pid;

error_log  /var/log/nginx/error.log;

events {

#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

use epoll;

#单个后台worker process进程的最大并发链接数

worker_connections  10240;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

include       mime.types;

default_type  application/octet-stream;

error_page 400 403 500 502 503 504  /50x.html;

index index.html index.shtml

autoindex off;

fastcgi_intercept_errors on;

sendfile        on;

# These are good default values.

tcp_nopush      on;

tcp_nodelay     off;

# output compression saves bandwidth

gzip  off;

#gzip_static on;

#gzip_min_length  1k;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_buffers  4 16k;

gzip_proxied any;

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

gzip_types  text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;

#gzip_vary on;

server_name_in_redirect off;

#设定负载均衡的服务器列表

upstream portals {

server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;

server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;

server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;

server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;

}

#upstream overflow {

#       server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;

#       server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;

#}

server {

#侦听8080端口

listen       8080;

server_name  127.0.0.1;

页面重定向地址

error_page  403 = http://www.e100.cn/ebiz/other/217/403.html;

error_page  404 = http://www.e100.cn/ebiz/other/218/404.html;

proxy_connect_timeout      90;

proxy_send_timeout         180;

proxy_read_timeout         180;

proxy_buffer_size 64k;

proxy_buffers 4 128k;

proxy_busy_buffers_size 128k;

client_header_buffer_size 16k;

large_client_header_buffers 4 64k;

#proxy_send_timeout         3m;

#proxy_read_timeout         3m;

#proxy_buffer_size          4k;

#proxy_buffers              4 32k;

proxy_set_header Host $http_host;

proxy_max_temp_file_size 0;

#proxy_hide_header Set-Cookie;

#       if ($host != 'www.e100.cn' ) {

#                rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;

#       }

location / {

deny all;

}

location ~ ^/resource/res/img/blue/space.gif {

proxy_pass http://tecopera;

}

location = / {

rewrite ^(.*)$  /ebiz/event/517.html last;

}

location = /ebiz/event/517.html {

add_header Vary Accept-Encoding;

root /data/web/html;

expires 10m;

}

location = /check.html {

root /usr/local/nginx/html/;

access_log off;

}

location = /50x.html {

root /usr/local/nginx/html/;

expires 1m;

access_log off;

}

location = /index.html {

add_header Vary Accept-Encoding;

#定义服务器的默认网站根目录位置

root /data/web/html/ebiz;

expires 10m;

}

#定义反向代理访问名称

location ~ ^/ecps-portal/* {

# expires 10m;

#重定向集群名称

proxy_pass http://portals;

#proxy_pass http://172.16.68.134:8082;

}

location ~ ^/fetionLogin/* {

# expires 10m;

proxy_pass http://portals;

#proxy_pass http://172.16.68.134:8082;

}

#location  ~ ^/business/* {

#   # expires 10m;

#    proxy_pass http://172.16.68.132:8088;

#    #proxy_pass http://172.16.68.134:8082;

#}

location ~ ^/rsmanager/* {

expires 10m;

root /data/web/;

#proxy_pass http://rsm;

}

#定义nginx处理的页面后缀

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

root /data/web/html/;

分钟

expires 10m;

}

#设定查看Nginx状态的地址

location ~* ^/NginxStatus/ {

stub_status on;

access_log off;

allow 10.1.252.126;

allow 10.248.6.49;

allow 127.0.0.1;

deny all;

}

#       error_page   405 =200 @405;

#       location @405

#       {

#                proxy_pass http://10.248.6.45:8080;

#       }

access_log  /data/logs/nginx/access.log combined;

error_log   /data/logs/nginx/error.log;

}

server {

listen       8082;

server_name  _;

location = /check.html {

root /usr/local/nginx/html/;

access_log off;

}

}

server {

listen       8088;

server_name  _;

location ~ ^/* {

root /data/web/b2bhtml/;

access_log off;

}

}

server {

listen       9082;

server_name  _;

#        location ~ ^/resource/* {

#            expires 10m;

#           root /data/web/html/;

#       }

location  / {

root /data/web/html/sysMaintain/;

if (!-f $request_filename) {

rewrite ^/(.*)$ /sysMaintain.html last;

}

}

}

}

==============================================================

<--avoid missing-->

管理 Servlet 的容器是 Context 容器,一个 Context 对应一个 Web 工程

<Context path="/projectOne " docBase="D:\projects\projectOne"
reloadable="true" />

Tomcat 增加一个 Web 工程时会增加一个Context ctx = new StandardContext();

Tomcat处理一个HTTP请求的过程
假设来自客户的请求为: http://localhost:8080/wsota/wsota_index.jsp 
1) 请求被发送到本机端口8080,被在那里侦听的Coyote HTTP/1.1 Connector获得 
2) Connector把该请求交给它所在的Service的Engine来处理,并等待来自Engine的回应 
3) Engine获得请求localhost/wsota/wsota_index.jsp,匹配它所拥有的所有虚拟主机Host 
4) Engine匹配到名为localhost的Host(即使匹配不到也把请求交给该Host处理,因为该Host被定义为该Engine的默认主机) 
5) localhost Host获得请求/wsota/wsota_index.jsp,匹配它所拥有的所有Context 
6) Host匹配到路径为/wsota的Context(如果匹配不到就把该请求交给路径名为""的Context去处理) 
7) path="/wsota"的Context获得请求/wsota_index.jsp,在它的mapping table中寻找对应的servlet 
8) Context匹配到URL PATTERN为*.jsp的servlet,对应于JspServlet类 
9) 构造HttpServletRequest对象和HttpServletResponse对象,作为参数调用JspServlet的doGet或doPost方法 
10)Context把执行完了之后的HttpServletResponse对象返回给Host 
11)Host把HttpServletResponse对象返回给Engine 
12)Engine把HttpServletResponse对象返回给Connector 
13)Connector把HttpServletResponse对象返回给客户browser

location是nginx用来处理对同一个server不同的请求地址使用独立的配置的方式
location path
cat /usr/local/nginx/conf/nginx.conf

nginx.conf:
user mobileweb mobileweb;
worker_processes 8;
error_log /home/mobileweb/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;

events
{
use epoll;
worker_connections 65535;
}

http
{
upstream backendjms #负载均衡
{
server 10.127.3.215:9250 max_fails=3 fail_timeout=20s;
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server {
listen 80 default_server;
server_name dcshi.com;
root www;
proxy_pass http://myserver;
location /break/ {
rewrite ^/break/(.*) /test/$1 break;
echo "break page";
}

location /last/ {
rewrite ^/last/(.*) /test/$1 last;
echo "last page";
}

location /test/ {
echo "test page";
}
}
}

语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

rewrite的生效区块为sever, location, if
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
/download/eva/media/op1.mp3 ->/download/eva/mp3/op1.mp3
if ($host ~* ^www\.(cafeneko\.info)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
不想自动追加query string
rewrite ^/users/(.*)$ /show?user=$1? last;

sever区块中如果有包含rewrite规则,则会最先执行,而且只会执行一次
然后再判断命中哪个location的配置,再去执行该location中的rewrite
当该location中的rewrite执行完毕时
rewrite并不会停止
而是根据rewrite过的URL再次判断location并执行其中的配置
last和break最大的不同在于
- last会重新发起一个新请求,并重新匹配location
- break是终止当前location的rewrite检测,而且不再进行location匹配
– last是终止当前location的rewrite检测,但会继续重试location匹配并处理区块中的rewrite规则

location /download/ {
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
} //10 ->500

Tomcat Nginx cluster note的更多相关文章

  1. Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享

    Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...

  2. Tomcat+Nginx+Memcached综合案例

    Tomcat+Nginx+Memcached综合案例 说明 通过Nginx解析静态页面并将动态负载均衡调度给后面的多个Tomcat,Tomcat解析java动态程序. 由于http是无状态的协议,你访 ...

  3. tomcat+nginx+redis实现均衡负载、session共享(一)

    在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一些状况: 1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一) ...

  4. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  5. 【转载】tomcat+nginx+redis实现均衡负载、session共享(一)

    http://www.cnblogs.com/zhrxidian/p/5432886.html 在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一 ...

  6. tomcat+nginx实现均衡负载

    在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一些状况: 1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一) ...

  7. tomcat nginx默许的post大小限制

    tomcat nginx默认的post大小限制 执行大文件上传,或者,大数据量提交时,当提交的数据大小超过一定限制时,发现后台从request取值的代码request.getParameter(&qu ...

  8. (转)tomcat+nginx+redis实现均衡负载、session共享(一)

    在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一些状况: 1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一) ...

  9. tomcat+nginx+redis集群试验

    Nginx负载平衡 + Tomcat + 会话存储Redis配置要点   使用Nginx作为Tomcat的负载平衡器,Tomcat的会话Session数据存储在Redis,能够实现0当机的7x24 运 ...

随机推荐

  1. PHP 文件系统管理函数与 preg_replace() 函数过滤代码

    案例:在带行号的代码至文件 crop.js 中.用两种方法去掉代码前面的行号,带行号的代码片段: 1.$(function(){ 2. //初始化图片区域 3. var myimg = new Ima ...

  2. 省略nslog打印

    //#if #endif宏定义的意思就是如果定义了DEBUG,那么就使用NSLog输出:否则这段代码直接忽略.有人会疑问这个DEBUG和_DEBUG来自哪里,这个其实不用担心,这个来自于Xcode的默 ...

  3. 个人翻译的cedec2010基于物理的光照

    作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广    本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...

  4. 撑起大规模PHP网站的开源工具

    撑起大规模PHP网站的开源工具 百万级PHP站点Poppen.de的架构 在 2011年11月27日 那天写的     已经有 3957 次阅读了 感谢 参考或原文   服务器君一共花费了54.510 ...

  5. hibernate 数据库列别名自动映射pojo属性名

    package com.pccw.business.fcm.common.hibernate; import java.lang.reflect.Field; import java.math.Big ...

  6. Android 4.2蓝牙介绍

    蓝牙一词源于公元十世纪丹麦国王HaraldBlatand名字中的Blatand.Blatand的英文之意就是Blue tooth.这是因为这位让丹麦人引以为傲的国王酷爱吃蓝莓以至于牙龈都被染成蓝色.由 ...

  7. docker-compose bug

    annot mount volume over existing file, file exists /var/lib/docker/aufs/mnt/0ac71fed1af802a4ecf4a93b ...

  8. isset和empty比较

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  9. BAT for循环

    一,数字循环 echo off & color 0A for /l %%i in (1,1,10) do ( echo %%i ) pause > nul 输出: 1 2 3 4 5 6 ...

  10. MVC从服务器端返回js到客户端的方法(总结)

    1.利用ViewBag,从服务器端创建一个显示js开关的ViewBag,然后到View中去做判断. Controller端 [HttpPost] public ActionResult Index(h ...