centos6.4
安装说明书
http://nginx.org/en/docs/install.html
【库依赖】
gzip 模块需要 zlib 库
rewrite 模块需要 pcre 库,自定义安装参考http://phl.iteye.com/blog/1979059
ssl 功能需要 openssl 库,自定义安装参考http://phl.iteye.com/blog/1979053
centOS安装依赖
yum -y install make gcc zlib zlib-devel openssl openssl-devel pcre pcre-devel
ubuntu安装依赖
apt-get install openssl libssl-dev gcc++ zlib1g-dev libpcre3-dev
对于nginx,如果依赖库自定义安装,需要额外参数
--with-pcre=/usr/local/pcre/default
--with-openssl=/root/openssl-1.0.0
等
【安装】
./configure --prefix=/application/search/nginx/nginx-1.4.4
make -j16
make install -j16
make clean -j16
注:其中-j参数是线程数,可以提高编译速度
【相关命令】
启动./nginx
平滑重启1 kill -HUP `cat logs/nginx.pid`
平滑重启2 kill ./nginx -s reload
停止服务 ./nginx -s stop
【常见问题】
nginx 403 forbidden 解决
在linux下 给文件夹加访问权限
如果还不行,给nginx.conf加上 user root; # 或者你当前的登录用户名
禁止输出core文件在sbin目录下
working_directory /dev/null
【1像素图片插件】
该插件在nginx1.4中,默认就安装的
http://wiki.nginx.org/HttpEmptyGifModule
配置语法
location = /_.gif {
empty_gif;
}
【https模块】
此模块默认不安装
安装教程
http://bbs.linuxtone.org/thread-3705-1-1.html
openssl生成证书
http://www.cnblogs.com/tintin1926/archive/2012/07/12/2587311.html
关键选项,其中--with-openssl=/root/openssl-1.0.0可以自定义制定openssl的安装位置
--with-http_ssl_module
编译安装
./configure \
--prefix=/application/search/nginx/nginx-1.4.4 \
--with-http_ssl_module
生成证书
#openssl genrsa -des3 -out server.key 1024
#openssl req -new -key server.key -out server.csr
#openssl rsa -in server.key -out server_nopwd.key
#openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt
同时拷贝证书到 conf/ssl目录下
配置文件,新增一个server节点,端口443
server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate ssl/server.crt;
ssl_certificate_key ssl/server_nopwd.key;
ssl_session_timeout 5m;
}
【tcp模块】
下载地址 https://github.com/yaoweibin/nginx_tcp_proxy_module
安装教程:http://cnodejs.org/topic/4f16442ccae1f4aa270010b3
nginx_tcp_proxy_module-0.26
patch -p1 < /application/soft/nginx_tcp_proxy_module-0.26/tcp.patch
关键选项
--add-module=/application/soft/nginx_tcp_proxy_module-0.26
编译示例
./configure \
--prefix=/application/usr/nginx/nginx-1.4.4 \
--add-module=/application/soft/nginx_tcp_proxy_module-0.26
【缓存插件】
官方地址:http://wiki.nginx.org/NginxChsCachePurge
下载地址:http://labs.frickle.com/nginx_ngx_cache_purge/
编译示例
./configure \
--prefix=/application/usr/nginx/nginx-1.3.9-2 \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--with-pcre=/application/usr/pcre/default \
--add-module=/application/soft/ngx_cache_purge-1.6
==================================================================================
负载均衡的推荐文章
http://www.cnblogs.com/daizhj/archive/2009/11/03/1595292.html
1)、轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)、weight :指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2)、ip_hash :每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3)、fair(第三方):按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)、url_hash(第三方)
【配置反向代理-地址池】
# 地址池
# 与server节点平级(或者include外部文件)
# 必须是IP地址,域名好像不行
upstream myserver {
server 10.10.128.54:8080;
server 127.0.0.1:88;
server 127.0.0.1:89;
#server 10.10.128.92:8080 max_fails=2 fail_timeout=30s;
#权重
#server 127.0.0.1:8080 weight=2;
#server 127.0.0.1:8081 weight=1;
#ip_hash;
}
【带负载均衡的反向代理-需要使用地址池】
location /load {
# 代理的目标
proxy_pass http://myserver/;
# response中,head中的"Location","Refresh",response.sendRedirect("hello.jsp")相对路径,静态文件相对路径文件引用
proxy_redirect http://myserver/ /;
# request.getServerName()的属性,如果不设置,直接返回 "myserver"字符串,不是其中负载均衡的变量;如果设置,则返回客户端请求的url中的域
proxy_set_header Host $host;
# 客户端链地址
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
}
【不带负载均衡】
#server节点配置(或者include外部文件)
location /google {
proxy_pass http://www.google.com/;
proxy_redirect default;
}
location /baidu {
proxy_pass http://www.baidu.com/;
proxy_redirect default;
}
【静态过滤配置】
# 就是静态文件不走反向代理,直接从nginx读取
# server节点配置(或者include外部文件)
# static 通过文件夹,其中^~ /static/是正则表达式,url中以这个开头
location ^~ /static/ {
root html;
}
# static 通过后缀名
location ~ .*\.(js|css|jpg|jpeg|png|bmp|swf)$ {
root html;
}
【alias与root的区别】
http://down.chinaz.com/server/201111/1382_1.htm
location ^~ /my/ {
alias /application/search/front/;
expires 1h;
add_header Cache-Control public;
access_log off;
}
【健康检查配置示例】
upstream server_pool_m1 {
ip_hash;
server 10.10.160.55:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.56:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.57:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.58:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.59:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.60:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.73:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.74:8282 max_fails=2 fail_timeout=30s;
server 10.10.160.75:8282 max_fails=2 fail_timeout=30s;
check interval=3000 rise=2 fall=1 timeout=10000 type=http;
check_http_send "GET /v1/search/web?url=http%3A%2F%2Fm.panguso.com%2Fsearch%2Fweb&from=active&adv=0&us=0&q=da+ HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
【get请求长度限制】
#nginx server节点
client_header_buffer_size 10m;
【POST上传限制】
#nginx server节点
client_max_body_size 500m;
【配置ico】
#server节点配置
location ~* \.(ico)$ {
if (-f $request_filename) {
expires 30d;
break;
}
root /application/search/favicon;
}
【禁用rest的部分请求】
#server节点配置(或者include外部文件)
if ($request_method = PUT ) {
return 403;
}
if ($request_method = DELETE ) {
return 403;
}
if ($request_method = OPTIONS ) {
return 403;
}
if ($request_method = TRACE ) {
return 403;
}
nginx系列(一)Linux下安装及基础配置
猜你喜欢
转载自phl.iteye.com/blog/1979082
今日推荐
周排行