nginx简配
- 格式:doc
- 大小:43.00 KB
- 文档页数:5
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个
IMAP/POP3/SMTP 代理服务器。
Nginx的优点:
1、高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2~3万并发连接数。
2、内存消耗少:在3万并发连接下,开启的10个Nginx进程才消耗150M内存。
3、配置文件简单:风格和程序一样通俗易懂。
4、成本低廉:Nginx为开源软件,开始免费使用。
5、支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组。
6、那只的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问。
7、节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头。
8、稳定性高:用于反向代理,宕机的概率微乎其微。
Nginx的主要应用类别
1、使用Nginx 结合FastCGI 运行 PHP 、 JSP 、 Perl 等程序。
2、使用Nginx 作反向代理、负载均衡、规则过滤。
3、使用Nginx 运行静态HTML页面、图片。
4、Nginx 与其他新技术的结合应用。
Nginx如何实现高并发:
I/O模型采用异步非阻塞的事件驱动机制,由进程循环处理多个准备好的事件,如epoll机制;
Nginx与Apache对高并发处理上的区别:
对于Apache,每个请求都会独占一个工作线程,当并发量增大时,也会产生大量的工作线程,导致内存占用急剧上升,同时线程的上下文切换也会导致CPU开销增大,导致在高并发场景下性能下降严重;
对于Nginx,一个worker进程只有一个主线程,通过事件驱动机制,实现循环处理多个准备好的事件,从而实现轻量级和高并发;
部署配置 1. 所需包
yum install gcc gcc-c++ make cmake ncurses ncurses-devel libxml2 libxml2-devel
openssl-devel bison bison-devel libevent pcre*
2. 切换到nginx的根目录下 运行 ./configure
3. 运行 make
4. 运行 make install
nginx 默认目录位于 /usr/local/nginx
安装 nginx 、pcre
[root@localhost pack]# tar zxf pcre-8.30.tar.gz
[root@localhost pack]# cd pcre-8.30
[root@localhost pcre-8.30]# ./configure --prefix=/usr/local/pcre ; make ; make install
[root@localhost pack]# groupadd nginx
[root@localhost pack]# useradd -r -s /sbin/nologin -g nginx nginx
[root@localhost pack]# tar zxf nginx-1.3.12.tar.gz
[root@localhost pack]# cd nginx-1.3.12
[root@localhost nginx-1.3.12]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx
--with-http_stub_status_module --with-http_ssl_module --with-http_flv_module
--with-http_gzip_static_module --with-google_perftools_module
--sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid --with-pcre=/usr/local/src/pack/pcre-8.30(源码路径)
[root@localhost nginx-1.3.12]#make ; make install
[root@localhost local]# grep -v '#' nginx/conf/nginx.conf | grep -v ^$
<=========================================================================================================>
user nginx nginx; ## 设定运行用户、组
worker_processes 1;
error_log /usr/local/nginx/logs/nginx_error.log crit; ## 错误日志路径
google_perftools_profiles /var/tmp/tcmalloc; ## 使用 TCMalloc 优化 nginx
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
} http { ## 利用反向代理功能提供负载均衡支持
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128; ## 设定请求缓存
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m; ## 设置客户端上传文件大小限制
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on; ## gzip 网页压缩
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server { ## 禁止IP访问站点
server_name _;
return 404;
}
server {
listen 80;
server_name ;
index index.html index.htm index.jsp index.do; ## 默认访问首页地址 root /data/abc/; ## 网站资源根路径
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location ~ \.(jsp|jspx|do)?$ { ## 设定 jsp 页面教由 resin 处理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;## 请求转向 resin
}
location
~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
## 设定静态文件直接读取
{
expires 30d;
}
location ~.*\.(js|css)?$
{
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
<=========================================================================================================>
[root@localhost local]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful