设为首页 收藏本站
查看: 1750|回复: 0

[经验分享] Nginx学习指南之安装与配置

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2014-5-8 09:24:36 | 显示全部楼层 |阅读模式
一、简介
Nginx [engine x]是Igor Sysoev编写的一个HTTP和反向代理服务器,另外它可以作为邮件代理服务器。Nginx已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括Yandex,Mail.Ru、VKontakte,以及Rambler,在国内使用Nginx作为Web Server,Cache Server,Proxy Server网站包括百度,新浪,腾讯,网易,机锋网等,根据Netcraft统计,在2012年8月份,世界上最繁忙的网站中有11.48使用Nginx作为其服务器或者代理服务器。

Nginx是少数写入地址的C10K问题之一的服务器,不同于传统的服务器的是Nginx不依赖于线程来处理请求。相反,它使用了一个更具扩展性的事件驱动(异步)架构。这种结构采用小,但更重要的是,内存负载下可预见的数额。即便你不希望处理成千上万的并发请求,你仍然可以受益于Nginx的高性能和小内存占用。然而促使这些大型互联网企业选择Nginx的原因是什么,不仅仅是因为Nginx开源,而是因为Nginx拥有更多的特性。
1、1 Nginx 特性
1>处理静态文件,索引文件以及自动索引,能打开文件描述符缓存。
2>作为前端缓存服务器,可以加速用户访问请求,减轻后端应用服务器压力。
3>作为反向代理服务器,根据源地址哈希,权重实现负载均衡以及健康检查的容错功能。
4>远程Fastcgi,uwsgi,SCGI,和memcached服务的缓存加速支持。
Note:了解更多特性,请参阅官方文档:http://nginx.org/cn/

二、Nginx工作原理示意图
wKioL1NqDlmRDSa6AAD0YVTFHik568.jpg
启动Nginx服务进程后,socket处于监听状态,当它侦听到一个HTTP请求时,Nginx Core通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
三、安装与配置3、1 下载
##提示:
在执行下面的操作前,你必须保证你的服务器或则VMware虚拟机能够连接互联网,不然你看到的只是错误的提示;
[iyunv@webserver1 ~]# mkdir /download/source -p
[iyunv@webserver1 ~]# cd /download/source/
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4311 (4.2K) [application/x-redhat-package-manager]
Saving to: °nginx-release-centos-6-0.el6.ngx.noarch.rpm±
100%[==========================================================================================>] 4,311       25.5K/s   in 0.2s  
2014-05-07 00:33:45 (25.5 KB/s) - °nginx-release-centos-6-0.el6.ngx.noarch.rpm± saved [4311/4311]
[iyunv@webserver1 source]# ll nginx-release-centos-6-0.el6.ngx.noarch.rpm
-rw-r--r-- 1 root root 4311 Oct 14  2011 nginx-release-centos-6-0.el6.ngx.noarch.rpm
3、2 RPM安装
##提示:
若使用Yum的方式安装,Nginx的功能是官方已经定制好的,如果你想使用nginx第三方模块,如Nginx网站加速模块ngx_pagespeed、文件上传模块nginx_upload_module、负载均衡模块nginx_upstream-fair,则需要下载Ngnx源码包后,附加上你需要的功能,然后编译安装;


[iyunv@webserver1 source]# yum install nginx
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Package nginx-1.6.0-1.el6.ngx.x86_64 already installed and latest version
Note:事先已经安装过,再次安装便会提示already install;
3、3 启动Nginx服务


[iyunv@webserver1 source]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[iyunv@webserver1 source]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State        PID/Program name
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      30063/nginx      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                  LISTEN      1910/sshd         
tcp        0      0 127.0.0.1:631               0.0.0.0:*                  LISTEN      1805/cupsd        
tcp        0      0 127.0.0.1:25                0.0.0.0:*                  LISTEN      2061/master      
tcp        0      0 :::139                      :::*                      LISTEN      29183/smbd
3、4 测试


## 本地测试
[iyunv@webserver1 source]# curl -I http://10.16.10.25/index.html
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Wed, 07 May 2014 08:06:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Apr 2014 13:32:20 GMT
Connection: keep-alive
ETag: "53591264-264"
Accept-Ranges: bytes
## Windows端firefox浏览器测试
wKiom1NqD7KQ9KYDAAJWiDXRnIc025.jpg
3、5 官方的编译参数
configure arguments: --prefix=/etc/nginx--sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid--lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp--http-proxy-temp-path=/var/cache/nginx/proxy_temp--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp--http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module--with-http_realip_module--with-http_addition_module --with-http_sub_module--with-http_dav_module --with-http_flv_module --with-http_mp4_module--with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module--with-http_secure_link_module --with-http_stub_status_module--with-http_auth_request_module --with-mail --with-mail_ssl_module--with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2-g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector--param=ssp-buffer-size=4 -m64 -mtune=generic'
四、Make编译安装4、1 下载
官方下载地址:http://nginx.org/download/nginx-1.6.0.tar.gz#稳定版


[iyunv@webserver1 ~]# mkdir /download/source -p
[iyunv@webserver1 ~]# cd /download/source/
[iyunv@webserver1 source]# wget http://nginx.org/download/nginx-1.6.0.tar.gz
--2014-05-07 01:28:18--  http://nginx.org/download/nginx-1.6.0.tar.gz
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 802956 (784K) [application/octet-stream]
Saving to: °nginx-1.6.0.tar.gz±
100%[==============================================================>] 802,956      145K/s   in 10s   
2014-05-07 01:28:29 (76.5 KB/s) - °nginx-1.6.0.tar.gz± saved [802956/802956]
[iyunv@webserver1 source]# ll nginx-1.6.0.tar.gz
-rw-r--r-- 1 root root 802956 Apr 24 06:03 nginx-1.6.0.tar.gz
4、2 安装
4、2、1 编译参数


[iyunv@webserver1 source]# tar –xf nginx-1.6.0.tar.gz
[iyunv@webserver1 source]# cd nginx-1.6.0
[iyunv@webserver1 nginx-1.6.0]# ./configure --prefix=/application/nginx-1.6.0 --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
[iyunv@webserver1 nginx-1.6.0]# make
[iyunv@webserver1 nginx-1.6.0]# make install
4、3 配置


[iyunv@webserver1 nginx-1.6.0]# ln -s /application/nginx-1.6.0/ /application/nginx
[iyunv@webserver1 nginx-1.6.0]# cd /application/nginx
[iyunv@webserver1 nginx]# groupadd nginx
[iyunv@webserver1 nginx]# useradd –r –g nginx nginx
[iyunv@webserver1 nginx]# vim ./conf/nginx.conf
user  nginx nginx;
worker_processes  8;
error_log  logs/error.log  notice;
pid        logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
    use epoll;
    worker_connections  65535;
}
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;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
keepalive_timeout  60;
# Load Nginx comperssion module
gzip  on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/css application/x-javascript text/xml application/xml text/javascript;
gzip_vary on;
server {
    listen       80;
    server_name  localhost;
   #charset koi8-r;
   charset utf-8;
location / {
            root   /data/bbs/program;
            index  index.html index.htm;
        }
   location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
       expires 1d;
   }
   location ~* \.(css|js)$ {
       expires 1h;
   }
   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;
   }
    …………………PHP,SSL部分省略……………………
}
4、4 检查语法,端口


[iyunv@webserver1 nginx]# mkdir /data/bbs/program/ -p
[iyunv@webserver1 nginx]# vim /data/bbs/program/index.html
<h1><center>Welcome to www.gfan.com site</center></h1>
[iyunv@webserver1 nginx]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.0/conf/nginx.conf test is successful
[iyunv@webserver1 nginx]# /application/nginx/sbin/nginx
[iyunv@webserver1 nginx]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                 LISTEN      37376/nginx
4、5 ulimit 调优
##提示
文件句柄数,建议与nginx配置文件的rlimit,conntion参数保持一致;


[iyunv@webserver1 nginx]# vim /etc/security/limits.conf              #在文件最后,添加以下两行;
*               soft    nofile          65535
*               hard    nofile          65535
[iyunv@webserver1 nginx]# ulimit -SHn 65535
[iyunv@webserver1 nginx]# ulimit -n
65535
4、6 测试


##本地测试
[iyunv@webserver1 nginx]# curl -I http://localhost/index.html                     
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Wed, 07 May 2014 09:42:06 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 38
Last-Modified: Wed, 07 May 2014 09:29:43 GMT
Connection: keep-alive
ETag: "5369fd07-26"
##Windows端firefox浏览器测试
wKioL1NqEMLxq0PeAAGS3tzEYE8924.jpg
##备注
此篇文档介绍Nginx两种最基础的安装方式与简单的优化过程,希望能带给大家一些帮助,后续会有更多关于Nginx的文章,不足之处,希望大家留言,一起交流,交流,共同进步,在此感谢。




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-18894-1-1.html 上篇帖子: 解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误 下篇帖子: nginx一些参数配置详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表