nginx 一路学习下来总结

[color=red][size=medium][b]为什么使用Nginx [/b][/size][/color]
1.高并发性 内部采用epoll的方式 而Apache则采用select方式
2.内存消耗少
3.成本低廉
4.配置简单
5.支持REWRITE对不同的URL进行不同的处理
6.因为可以采用GZIP 所以耗费的宽带较少
7.支持热部署

[size=medium][color=red][b]安装成功后可以输入 localhost来进行测试[/b][/color][/size]
使用域名时需要在C:\Windows\System32\drivers\etc\hosts文件下添加域名
如:
[color=red][b]127.0.0.1 xxxx.com[/b][/color]

[size=medium][color=blue][b]图片或者页面静态信息放入html或images文件夹下时 需要注意是否要整个项目加进去还是单个文件夹[/b][/color]
[/size]
[color=blue][b]如果有url有项目名 则需要加入整个项目 如果没有则单个文件夹就行[/b][/color]

location ~ .*\.(gif|jpeg|jpg|png)${
root images
}

[size=medium][color=red][b]NGINX可以对URL进行过滤操作 比如url中包含lan的则重定向rewrite到相应的页面[/b][/color][/size]
location ~ lan
rewrite lan /index.html;# [color=red][b]其中index.html是项目下面的index.html页面[/b][/color]
}


[color=red][size=medium][b]Linux下安装 Nginx[/b][/size][/color]
tar -zxvf nginx-xx.tar.gz
cd nginx-xx
./configure
make
sudo make install

默认安装到 /usr/local/nginx


  -- 信号量 平滑启动
kill -HUP Nginx进程号 --读取新的配置文件如果成功则运行新的工作进程 然后从容的关闭旧的工作进程
--如果失败则继续使用旧的工作进程
--Nginx支持以下信号量
kill -TERM,INT Nginx进程号 -- 快速关闭
kill -HUP Nginx进程号 -- 平滑重启
kill -QUIT Nginx进程号 -- 从容关闭
kill -USER1 Nginx进程号 --重新打开日志文件 用于日志切割
kill -USER2 Nginx进程号 --平滑升级可执行文件


--NGINX 配置文件 nginx.conf


--使用的用户和组
#user nobody;
--指定工作衍生进程数
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 {
--允许的连接数
worker_connections 1024;
}
http {
include mime.types; --设定MIME类型 在mime.types文件中
default_type application/octet-stream;
--与Nginx相关的两个指令 log_format(日志格式)格式不能重复 和access_log(日志存放路径)
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' --remote_addr表示反向代理服务器的地址
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
--remote_addr nginx服务器IP地址 $request请求响应类型 POST 或 GET $status 请求状态 $body_bytes_sent文件内容大小
--"$http_referer"请求的主体 $http_user_agent 浏览器信息
access_log logs/access.log main;

sendfile on;--使用sendfile函数进行输出文件
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;--连接超时时间
select * from test ;
--设置分发器
upstream local_tomcat{
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=3;
#server 127.0.0.1:8088 weight=1 max_fails=2 fail_timeout=3;
}
#设置虚拟主机
server {
listen 8000; # 设置端口号
server_name w.com; #设置域名 必须在host文件配置域名
# 使用gzip压缩响应数据
gzip on;
gzip_buffers 32 4k ;
# 200表示200B
gzip_min_length 200;
gzip_comp_level 6;
# 规定响应时压缩的文件类型
gzip_types text/css text/xml application/javascript;
location / {
proxy_pass http://local_tomcat;
}
location ~ .*\.(gif|jpeg|jpg|png)${
expires 30d
}
}
}

--Nginx日志切割
mv /data/log/access.log /data/log/20150707.log --把名字改成每天的时间
kill -USER1 `cat /data/logs/nginx.pid` --然后使用信号量 进行重新生成一份

--如果需要按天进行切割 则需要用到 crontab进行配置

--使用gzip进行压缩输出
http{
gzip on;
gzip_buffers 32 4k ;
# 200表示200B 小于200B则不进行压缩
gzip_min_length 200;
gzip_comp_level 6;
# 规定响应时压缩的文件类型
gzip_types text/css text/xml application/javascript;
}
--Nginx缓存设置 expires 作用域 http server location

--对项目中修改较少的文件进行设置 如图片设置

location ~ .*\.(jpg|gif|jpeg|png)${
expires 30d ;
}

--Nginx和Tomcat的配置

--静态html 图片 js css flash文件 使用Nginx来处理 .jsp .do 由反向代理 Tomcat HTTP服务器处理


--负载均衡 多台服务器以对称的方式组成一个服务器集合
--反向代理 以代理服务器来接受Internet上的请求 代理服务器对外就表现成一种服务器

--负载均衡配置

upstream 用于设置一组可以在 proxy_pass访问的服务器

upstream 是负载均衡的主要模块 使用环境 http 中

upstream local_tomcat{
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=3;
server 127.0.0.2:8088 weight=1 max_fails=2 fail_timeout=3;
}

server{
location / {
proxy_pass http://local_tomcat ;
}
}

# 动态的则访问 tomcat服务器
location ~ .*\.(jsp|action)$ {
proxy_pass http://local_tomcat;
}
## 静态资源则用代理服务器直接返回
location ~ .*\.(jpg|gif|jpeg|png|js|html|swf)$ { -- \ 表示的意思是把下一个字符当做特殊字符来处理 即转义字符
root html ; -- 把web部署代码拷贝一份到html文件夹下 即可(mms整个项目)
} --类似访问路径 D:\Nginx\nginx-1.6.0/html/cmbexam/static/images/icon/param.png

--Rewrite的功能主要是实现URL的重写
--能够根据不同的域名和URL 将不同的HTTP请求转发到后端服务器群组
Rewrite 规则相关的指令 rewrite if set return break

= 表示等于 !=表示不等于
~ 表示 区分大小写的匹配 ~* 不区分大小写的匹配 -- ~ 匹配正则表达式
-f 判断文件是否存在 -d 判断文件夹是否存在
-e 判断文件或文件夹是否存在 -x 判断文件是否可执行

return 用于把状态码返回给客户端 如 400 404 500 510等

location .*\.(sh|bash)$ {
return 403 ;
}
location ~ .*\.(lan|wei|xing)$ {
rewrite .*\.(lan|wei|xing)$ /index.html ; --rewrite 有两个参数 从一个URL跳转到另一个URL
--index.html 表示在html目录下的index.html文件
}
-- rewrite命令根据表达式来 重定向URI或修改字符串
-- rewrite命令使用环境 server location if

rewrite URI1 URI2 break ; --从URI1重定向到URI2 break表示本条匹配完后终止后面的匹配
return 403 ;

--last break 在重定向时地址栏 URL不会发生改变

last 表示rewrite完成 匹配完后会重新对所在的server发起请求

一般在根location使用last 而在非根location中使用break

--rewrite在使用{}进行正则表达式时应该使用双引号 ""

rewrite " ^/photo/([0-9]{2}) " /path/to/$1/$1$2.png

--ip_hash指令能够将某个客户端的IP进行哈希算法然后定位到后端同一台服务器,它无法保证负载均衡
--如果要摘除某个服务器则 可以使用 down进行处理
-- 设置的权重则不起作用
upstream up_local {
ip_hash ;
server xxx.xxx01.com ;
server xxx.xxx02.com ;
server xxx.xxx03.com down ; --如果使用down则还是按照3台计算机的算法进行分配 如果是注释一台则
--采用2台计算机的分配方式
}


[color=red]如果不采用ip_hash进行分发则需要考虑SESSION共享的问题[/color]
Nginx建议如果能解决应用服务器SESSION共享问题则[color=red]还是不采用ip_hash的方式[/color]
[b]ip_hash不能解决负载均衡[/b]

[color=red]如果考虑使用权重来解决负载均衡 则必须把SESSION共享问题处理。。[/color]
权重越高 分配到的客户端请求数就越多

双机高可用 实现故障转移和高可用
一台主服务器(提供负载均衡) 一台备热服务器(空闲状态)



#user nobody;
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 {
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 local_tomcat{
server localhost:8088 weight=5;
server localhost:8089 weight=5;
}
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

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

#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 ~ \.jsp$ {
root html;
fastcgi_pass 127.0.0.1:8089;
fastcgi_index index.jsp;
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; # 设置端口号
server_name z.com; #设置域名

location / {
root html; # 设置目录 nginx主目录下的html目录
index z.html index.htm; # 定位的页面
}
location ~ \.jsp$ {
proxy_pass http://local_tomcat;
}
}

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

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

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值