Nginx

实现WWW服务的常用web软件:nginx、apache。

经典流行的web组合lamp(linux apache mysql php)、lnmp(linux nginx mysql php)。

Nginx本身是一款静态(html、js、css、jpg等)的www软件,不能解析动态的PHP、JSP、DO。

最大的特点:

1、静态小文件(1M),支持高并发,同时占用的资源很少。30000并发,10个进程,内存消耗150M。

Nginx在windows、linux、unix都可以。

Nginx服务最大的功能:

1、www web服务、邮件服务、邮件代理。

2、负载均衡(反向代理proxy)。

3、web cache(web缓存),squid(CDN主要使用squid)。

Nginx特点:

1、配置简单、灵活、轻量。

2、高并发(静态小文件),静态几万的并发。

3、占用资源少。

4、功能种类比较多(web、cache、proxy),每一功能都不是特别强。

5、支持epoll模型。使得nginx可以支持高并发。apache使用select模型。

6、nginx可以配合动态PHP服务(fastcgi接口)。

7、利用nginx可以对IP限速,可限制连接数。

Nginx的应用场合

1、提供静态服务(图片、视频服务),另一个lighttpd。几万并发。

2、提供动态服务,nginx+fastcgi的方式运行PHP、JSP。动态并发500-1500。(apache+php、lighttpd+fastcgi php)

3、反向代理、负载均衡。日PV2000万以下,都可以直接用nginx做反向代理。(haproxy、F5、A10)

4、缓存服务。类似squid、varnish、ats。

nginx支持虚拟主机

一个server标签段就是一个虚拟主机。

1、基于域名的虚拟主机。通过域名来区分虚拟主机。应用:外部网站。

2、基于端口的虚拟主机。通过端口来区分虚拟主机。应用:公式内部网站,网站后台。

3、基于IP的虚拟主机。几乎不用。不支持ifconfig别名,配置文件可以。

Nginx安装

安装PCRE

Pcre全称(Perl Compatible Regular Expressions),中文perl兼容正则表达式。

  1. [root@lnmp ~]# cat /etc/redhat-release
  2. CentOS release 6.6 (Final)
  3. [root@lnmp ~]# uname -r
  4. 2.6.32-504.el6.x86_64
  5. [root@lnmp ~]# uname -m
  6. x86_64
  7. [root@lnmp ~]# rpm -qa pcre pcre-devel
  8. pcre-7.8-6.el6.x86_64
  9. [root@lnmp ~]# yum install pcre pcre-devel -y
  10. [root@lnmp ~]# rpm -qa pcre pcre-devel
  11. pcre-7.8-7.el6.x86_64
  12. pcre-devel-7.8-7.el6.x86_64

安装OpenSSL

  1. [root@lnmp ~]# rpm -qa openssl openssl-devel
  2. openssl-1.0.1e-30.el6.x86_64
  3. [root@lnmp ~]# yum install openssl-devel -y
  4. [root@lnmp ~]# rpm -qa openssl openssl-devel
  5. openssl-devel-1.0.1e-48.el6_8.3.x86_64
  6. openssl-1.0.1e-48.el6_8.3.x86_64

安装nginx

  1. [root@lnmp nginx-1.6.3]# useradd nginx -s /sbin/nologin -M
  2. [root@lnmp nginx-1.6.3]# id nginx
  3. uid=503(nginx) gid=503(nginx) groups=503(nginx)
  4. [root@lnmp ~]# cd /home/oldboy/tools/
  5. [root@lnmp tools]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
  6. [root@lnmp tools]# ls nginx-1.6.3.tar.gz -lk
  7. -rw-r--r--. 1 root root 787 Apr 8 2015 nginx-1.6.3.tar.gz
  8. [root@lnmp tools]# tar xf nginx-1.6.3.tar.gz
  9. [root@lnmp tools]# cd nginx-1.6.3
  10. [root@lnmp nginx-1.6.3]# ll
  11. total 624
  12. drwxr-xr-x. 6 1001 1001 4096 Feb 16 02:42 auto
  13. -rw-r--r--. 1 1001 1001 236608 Apr 7 2015 CHANGES
  14. -rw-r--r--. 1 1001 1001 360501 Apr 7 2015 CHANGES.ru
  15. drwxr-xr-x. 2 1001 1001 4096 Feb 16 02:42 conf
  16. -rwxr-xr-x. 1 1001 1001 2369 Apr 7 2015 configure
  17. drwxr-xr-x. 4 1001 1001 4096 Feb 16 02:42 contrib
  18. drwxr-xr-x. 2 1001 1001 4096 Feb 16 02:42 html
  19. -rw-r--r--. 1 1001 1001 1397 Apr 7 2015 LICENSE
  20. drwxr-xr-x. 2 1001 1001 4096 Feb 16 02:42 man
  21. -rw-r--r--. 1 1001 1001 49 Apr 7 2015 README
  22. drwxr-xr-x. 8 1001 1001 4096 Feb 16 02:42 src
  23. [root@lnmp nginx-1.6.3]# tree|wc -l
  24. 404
  25. [root@lnmp nginx-1.6.3]# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_stub_status_module
  26. [root@lnmp nginx-1.6.3]# echo $?
  27. 0
  28. [root@lnmp nginx-1.6.3]# make && make install
  29. [root@lnmp nginx-1.6.3]# cd ..
  30. [root@lnmp tools]# ll /application/nginx-1.6.3/ -ld
  31. drwxr-xr-x. 6 root root 4096 Feb 16 02:50 /application/nginx-1.6.3/
  32. [root@lnmp tools]# ln -s /application/nginx-1.6.3/ /application/nginx
  33. [root@lnmp tools]# ls -l /application/
  34. total 4
  35. lrwxrwxrwx. 1 root root 25 Feb 16 02:53 nginx -> /application/nginx-1.6.3/
  36. drwxr-xr-x. 6 root root 4096 Feb 16 02:50 nginx-1.6.3

启动nginx

  1. [root@lnmp tools]# /application/nginx/sbin/nginx
  2. [root@lnmp tools]# ps -ef|grep nginx|grep -v grep
  3. root 1362 1231 0 01:44 pts/0 00:00:00 wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
  4. root 1364 1231 0 01:46 pts/0 00:00:00 wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
  5. root 3853 1 0 02:59 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
  6. nginx 3854 3853 0 02:59 ? 00:00:00 nginx: worker process
  7. [root@lnmp tools]# ss -lntup|grep nginx
  8. tcp LISTEN 0 128 *:80 *:* users:(("nginx",3853,6),("nginx",3854,6))
  9. [root@lnmp tools]# curl 127.0.0.1
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <title>Welcome to nginx!</title>
  14. <style>
  15.     body {
  16.         width: 35em;
  17.         margin: 0 auto;
  18.         font-family: Tahoma, Verdana, Arial, sans-serif;
  19.     }
  20. </style>
  21. </head>
  22. <body>
  23. <h1>Welcome to nginx!</h1>
  24. <p>If you see this page, the nginx web server is successfully installed and
  25. working. Further configuration is required.</p>
  26.  
  27. <p>For online documentation and support please refer to
  28. <a href="http://nginx.org/">nginx.org</a>.<br/>
  29. Commercial support is available at
  30. <a href="http://nginx.com/">nginx.com</a>.</p>
  31.  
  32. <p><em>Thank you for using nginx.</em></p>
  33. </body>
  34. </html>

浏览器访问

  1. [root@lnmp nginx]# ls -l|grep -v temp
  2. total 36
  3. drwxr-xr-x. 2 root root 4096 Feb 16 02:50 conf #配置
  4. drwxr-xr-x. 2 root root 4096 Feb 16 02:50 html #默认网站目录
  5. drwxr-xr-x. 2 root root 4096 Feb 16 02:59 logs #错误,访问日志
  6. drwxr-xr-x. 2 root root 4096 Feb 16 02:50 sbin #启动命令

部署站点

新建一个index2.html。

  1. [root@lnmp html]# cat index2.html
  2. <html>
  3. <head><title>Golden,s Nginx server blog.</title></head>
  4. <body>
  5. Hi,I am golden.My blog address is
  6. <a href="http://www.cnblogs.com/yinshoucheng-golden">http://www.cnblogs.com/yinshoucheng-golden</a>
  7. </body>
  8. </html>

浏览器访问:http://192.168.31.134/index2.html

Nginx http功能模块汇总

ngx_http_core_module:包括一些核心的http参数配置,对应nginx的配置为http区块部分。

ngx_http_access_module:访问控制模块,用来控制网站用户对nginx的访问。

ngx_http_gzip_module:压缩模块,对nginx返回的数据压缩,属于性能优化模块。

ngx_http_fastcgi_module:fastcgi模块和动态应用相关的模块,例如PHP。

ngx_http_proxy_module:proxy代理模块。

ngx_http_upstream_module:负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查。

ngx_http_rewrite_module:URL地址重写模块。

ngx_http_limit_conn_module:限制用户并发连接数及请求数模块。

ngx_http_limit_req_module:限制Nginx request processing rate根据定义的key。

ngx_http_log_module:访问日志模块,以指定的格式记录nginx客户访问日志等信息。

ngx_http_anth_basic_module:web认证模块,设置web用户通过账号密码访问nginx。

ngx_http_ssl_module:ssl模块,用于加密的http连接,如https。

ngx_http_stub_status_module:记录nginx基本访问状态信息等的模块。

nginx.conf

  1. [root@lnmp conf]# cat nginx.conf
  2.  
  3. #user nobody;
  4. worker_processes 1; # 4 – 10行main区,nginx核心功能模块
  5.  
  6. #error_log logs/error.log;
  7. #error_log logs/error.log notice;
  8. #error_log logs/error.log info;
  9.  
  10. #pid logs/nginx.pid;
  11.  
  12.  
  13. events { # 13 – 15 行 events区,nginx核心功能模块
  14.     worker_connections 1024;
  15. }
  16.  
  17.  
  18. http { # http区,nginx http核心模块
  19.     include mime.types;
  20.     default_type application/octet-stream;
  21.  
  22.     #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  23.     # '$status $body_bytes_sent "$http_referer" '
  24.     # '"$http_user_agent" "$http_x_forwarded_for"';
  25.  
  26.     #access_log logs/access.log main;
  27.  
  28.     sendfile on;
  29.     #tcp_nopush on;
  30.  
  31.     #keepalive_timeout 0;
  32.     keepalive_timeout 65;
  33.  
  34.     #gzip on;
  35.  
  36.     server { # server标签
  37.         listen 80; # 端口
  38.         server_name localhost; # 域名
  39.  
  40.         #charset koi8-r;
  41.  
  42.         #access_log logs/host.access.log main;
  43.  
  44.         location / { # 根据uri进行跳转
  45.             root html;
  46.             index index.html index.htm; # 首页
  47.         }
  48.  
  49.         #error_page 404 /404.html;
  50.  
  51.         # redirect server error pages to the static page /50x.html
  52.         #
  53.         error_page 500 502 503 504 /50x.html;
  54.         location = /50x.html {
  55.             root html;
  56.         }
  57.  
  58.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  59.         #
  60.         #location ~ \.php$ {
  61.         # proxy_pass http://127.0.0.1;
  62.         #}
  63.  
  64.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  65.         #
  66.         #location ~ \.php$ {
  67.         # root html;
  68.         # fastcgi_pass 127.0.0.1:9000;
  69.         # fastcgi_index index.php;
  70.         # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  71.         # include fastcgi_params;
  72.         #}
  73.  
  74.         # deny access to .htaccess files, if Apache's document root
  75.         # concurs with nginx's one
  76.         #
  77.         #location ~ /\.ht {
  78.         # deny all;
  79.         #}
  80.     }
  81.  
  82.  
  83.     # another virtual host using mix of IP-, name-, and port-based configuration
  84.     #
  85.     #server {
  86.     # listen 8000;
  87.     # listen somename:8080;
  88.     # server_name somename alias another.alias;
  89.  
  90.     # location / {
  91.     # root html;
  92.     # index index.html index.htm;
  93.     # }
  94.     #}
  95.  
  96.  
  97.     # HTTPS server
  98.     #
  99.     #server {
  100.     # listen 443 ssl;
  101.     # server_name localhost;
  102.  
  103.     # ssl_certificate cert.pem;
  104.     # ssl_certificate_key cert.key;
  105.  
  106.     # ssl_session_cache shared:SSL:1m;
  107.     # ssl_session_timeout 5m;
  108.  
  109.     # ssl_ciphers HIGH:!aNULL:!MD5;
  110.     # ssl_prefer_server_ciphers on;
  111.  
  112.     # location / {
  113.     # root html;
  114.     # index index.html index.htm;
  115.     # }
  116.     #}
  117.  
  118. }

 

  1. [root@lnmp conf]# egrep -v "#|^$" nginx.conf.default
  2. worker_processes 1;
  3. events {
  4.     worker_connections 1024;
  5. }
  6. http {
  7.     include mime.types;
  8.     default_type application/octet-stream;
  9.     sendfile on;
  10.     keepalive_timeout 65;
  11.     server {
  12.         listen 80;
  13.         server_name localhost;
  14.         location / {
  15.             root html;
  16.             index index.html index.htm;
  17.         }
  18.         error_page 500 502 503 504 /50x.html; #出现对应的http状态码使用50x.html回应
  19.         location = /50x.html { #location区块开始,访问50x.html
  20.             root html; #指定对应的站点目录为html
  21.         }
  22.     }
  23. }
  24. [root@lnmp conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf

搭建基于域名的虚拟主机

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1; #worker进程的数量
  3. events { #事件区块开始
  4.     worker_connections 1024; #每个worker进程支持的最大连接数
  5. } #事件区块开始
  6. http { #http区块开始
  7.     include mime.types; #nginx支持的媒体类型库文件包含
  8.     default_type application/octet-stream; #默认的媒体类型
  9.     sendfile on; #开启高效传输模式
  10.     keepalive_timeout 65; #连接超时
  11.     server { #第一个server区块开始,表示一个独立的虚拟主机站点
  12.         listen 80; #提供服务的端口,默认80
  13.         server_name www.etiantian.org; #提供服务的域名主机名
  14.         location / { #第一个location区块开始
  15.             root html/www; #站点的根目录,相对于nginx安转目录
  16.             index index.html index.htm; #默认的首页文件,多个用空格分开
  17.         } #第一个localtion区块结果
  18.     }
  19.     server {
  20.         listen 80;
  21.         server_name bbs.etiantian.org;
  22.         location / {
  23.             root html/bbs;
  24.             index index.html index.htm;
  25.         }
  26.     }
  27.  
  28. }

 

  1. [root@lnmp conf]# mkdir ../html/{www,bbs} -p
  2. [root@lnmp conf]# tree ../html/
  3. ../html/
  4. ── 50x.html
  5. ── bbs
  6. ── index2.html
  7. ── index.html
  8. └── www
  9. [root@lnmp conf]# echo "www.etiantian.org" >../html/www/index.html
  10. [root@lnmp conf]# echo "bbs.etiantian.org" >../html/bbs/index.html
  11. [root@lnmp conf]# cat ../html/{www,bbs}/index.html
  12. www.etiantian.org
  13. bbs.etiantian.org
  14. [root@lnmp conf]# /application/nginx/sbin/nginx -t #检查语法
  15. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  16. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  17. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload #重启
  18. [root@lnmp conf]# cat /etc/hosts
  19. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  20. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  21.  
  22. 192.168.31.132 server
  23. 192.168.31.133 lamp
  24. 192.168.31.134 lnmp www.etiantian.org bbs.etiantian.org
  25. 192.168.31.136 backup
  26.  
  27. [root@lnmp conf]# ping www.etiantian.org
  28. PING lnmp (192.168.31.134) 56(84) bytes of data.
  29. 64 bytes from lnmp (192.168.31.134): icmp_seq=1 ttl=64 time=0.084 ms
  30. 64 bytes from lnmp (192.168.31.134): icmp_seq=2 ttl=64 time=0.050 ms
  31. ^Z
  32. [5]+ Stopped ping www.etiantian.org
  33. [root@lnmp conf]# ping bbs.etiantian.org
  34. PING lnmp (192.168.31.134) 56(84) bytes of data.
  35. 64 bytes from lnmp (192.168.31.134): icmp_seq=1 ttl=64 time=0.034 ms
  36. 64 bytes from lnmp (192.168.31.134): icmp_seq=2 ttl=64 time=0.050 ms
  37. ^Z
  38. [6]+ Stopped ping bbs.etiantian.org
  39. [root@lnmp conf]# curl www.etiantian.org
  40. www.etiantian.org
  41. [root@lnmp conf]# curl bbs.etiantian.org
  42. bbs.etiantian.org

通过浏览器访问需要修改windows hosts文件(C:\Windows\System32\drivers\etc),加入192.168.31.134 www.etiantian.org bbs.etiantian.org。

基于域名的虚拟主机配置步骤

1、修改配置文件nginx.conf。

2、创建站点目录

3、检查语法,重新加载nginx。

4、配置hosts,测试。

基于端口的虚拟主机

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3. events {
  4.     worker_connections 1024;
  5. }
  6. http {
  7.     include mime.types;
  8.     default_type application/octet-stream;
  9.     sendfile on;
  10.     keepalive_timeout 65;
  11.     server {
  12.         listen 8001;
  13.         server_name www.etiantian.org;
  14.         location / {
  15.             root html/www;
  16.             index index.html index.htm;
  17.         }
  18.     }
  19.     server {
  20.         listen 8002;
  21.         server_name www.etiantian.org;
  22.         location / {
  23.             root html/bbs;
  24.             index index.html index.htm;
  25.         }
  26.     }
  27.    server {
  28.         listen 8003;
  29.         server_name www.etiantian.org;
  30.         location / {
  31.             root html/blog;
  32.             index index.html index.htm;
  33.         }
  34.     }
  35.  
  36. }

 

  1. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  4. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
  5. [root@lnmp conf]# netstat -lntup|grep nginx
  6. tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 3853/nginx
  7. tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 3853/nginx
  8. tcp 0 0 0.0.0.0:8003 0.0.0.0:* LISTEN 3853/nginx
  9. [root@lnmp conf]# curl http://www.etiantian.org:8001
  10. www.etiantian.org
  11. [root@lnmp conf]# curl http://www.etiantian.org:8002
  12. bbs.etiantian.org
  13. [root@lnmp conf]# curl http://www.etiantian.org:8003
  14. blog.etiantian.org

基于IP的虚拟主机

  1. [root@lnmp conf]# ip addr add 192.168.31.135/24 dev eth0
  2. [root@lnmp conf]# ip addr
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  4.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5.     inet 127.0.0.1/8 scope host lo
  6.     inet6 ::1/128 scope host
  7.        valid_lft forever preferred_lft forever
  8. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  9.     link/ether 00:0c:29:03:06:08 brd ff:ff:ff:ff:ff:ff
  10.     inet 192.168.31.134/24 brd 192.168.31.255 scope global eth0
  11.     inet 192.168.31.135/24 scope global secondary eth0
  12.     inet6 fe80::20c:29ff:fe03:608/64 scope link
  13.        valid_lft forever preferred_lft forever
  14. [root@lnmp conf]# cat nginx.conf
  15. worker_processes 1;
  16. events {
  17.     worker_connections 1024;
  18. }
  19. http {
  20.     include mime.types;
  21.     default_type application/octet-stream;
  22.     sendfile on;
  23.     keepalive_timeout 65;
  24.     server {
  25.         listen 192.168.31.134:80;
  26.         server_name www.etiantian.org;
  27.         location / {
  28.             root html/www;
  29.             index index.html index.htm;
  30.         }
  31.     }
  32.     server {
  33.         listen 192.168.31.135:80;
  34.         server_name www.etiantian.org;
  35.         location / {
  36.             root html/bbs;
  37.             index index.html index.htm;
  38.         }
  39.     }
  40. }
  41. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  42. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  43. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  44. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
  45. [root@lnmp conf]# curl 192.168.31.134
  46. www.etiantian.org
  47. [root@lnmp conf]# curl 192.168.31.135
  48. bbs.etiantian.org

删除添加的IP

  1. [root@lnmp conf]# ip addr del 192.168.31.135/24 dev eth0
  2. [root@lnmp conf]# ip addr
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  4.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5.     inet 127.0.0.1/8 scope host lo
  6.     inet6 ::1/128 scope host
  7.        valid_lft forever preferred_lft forever
  8. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  9.     link/ether 00:0c:29:03:06:08 brd ff:ff:ff:ff:ff:ff
  10.     inet 192.168.31.134/24 brd 192.168.31.255 scope global eth0
  11.     inet6 fe80::20c:29ff:fe03:608/64 scope link
  12.        valid_lft forever preferred_lft forever

 

posted on 2017-02-16 22:23  yinshoucheng  阅读(494)  评论(1)    收藏  举报

导航