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

  1. 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

  1. uid=503(nginx) gid=503(nginx)
    groups=503(nginx)

  2. [root@lnmp ~]# cd
    /home/oldboy/tools/

  3. [root@lnmp tools]# wget -q
    http://nginx.org/download/nginx-1.6.3.tar.gz

  1. [root@lnmp tools]# ls
    nginx-1.6.3.tar.gz -lk

  2. -rw-r–r–. 1 root root 787 Apr 8 2015
    nginx-1.6.3.tar.gz

  3. [root@lnmp tools]# tar xf
    nginx-1.6.3.tar.gz

  4. [root@lnmp tools]# cd nginx-1.6.3

  1. [root@lnmp nginx-1.6.3]# ll

  2. total 624

  3. drwxr-xr-x. 6 1001 1001 4096 Feb 16
    02:42 auto

  4. -rw-r–r–. 1 1001 1001 236608 Apr 7
    2015 CHANGES

  5. -rw-r–r–. 1 1001 1001 360501 Apr 7
    2015 CHANGES.ru

  6. drwxr-xr-x. 2 1001 1001 4096 Feb 16
    02:42 conf

  7. -rwxr-xr-x. 1 1001 1001 2369 Apr 7 2015
    configure

  8. drwxr-xr-x. 4 1001 1001 4096 Feb 16
    02:42 contrib

  9. drwxr-xr-x. 2 1001 1001 4096 Feb 16
    02:42 html

  10. -rw-r–r–. 1 1001 1001 1397 Apr 7 2015
    LICENSE

  11. drwxr-xr-x. 2 1001 1001 4096 Feb 16
    02:42 man

  12. -rw-r–r–. 1 1001 1001 49 Apr 7 2015
    README

  13. drwxr-xr-x. 8 1001 1001 4096 Feb 16
    02:42 src

  14. [root@lnmp nginx-1.6.3]# tree|wc -l

  1. 404

  2. [root@lnmp nginx-1.6.3]# ./configure
    –prefix=/application/nginx-1.6.3 –user=nginx –group=nginx
    –with-http_stub_status_module

  3. [root@lnmp nginx-1.6.3]# echo $?

  1. 0

  2. [root@lnmp nginx-1.6.3]# make &&
    make install

  3. [root@lnmp nginx-1.6.3]# cd ..

  1. [root@lnmp tools]# ll
    /application/nginx-1.6.3/ -ld

  2. drwxr-xr-x. 6 root root 4096 Feb 16
    02:50 /application/nginx-1.6.3/

  3. [root@lnmp tools]# ln -s
    /application/nginx-1.6.3/ /application/nginx

  4. [root@lnmp tools]# ls -l
    /application/

  5. total 4

  6. lrwxrwxrwx. 1 root root 25 Feb 16 02:53
    nginx -> /application/nginx-1.6.3/

  7. 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

  1. root 1364 1231 0 01:46 pts/0 00:00:00
    wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
  1. root 3853 1 0 02:59 ? 00:00:00 nginx:
    master process /application/nginx/sbin/nginx

  2. nginx 3854 3853 0 02:59 ? 00:00:00
    nginx: worker process

  3. [root@lnmp tools]# ss -lntup|grep
    nginx

  4. tcp LISTEN 0 128 *:80 *:*
    users:((“nginx”,3853,6),(“nginx”,3854,6))

  5. [root@lnmp tools]# curl 127.0.0.1

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <title>Welcome to
    nginx!</title>

  5. <style>

  6.     body {

  7.         width: 35em;

  8.         margin: 0 auto;

  9.         font-family: Tahoma, Verdana,
    Arial, sans-serif;

  10.     }

  11. </style>

  12. </head>

  13. <body>

  14. <h1>Welcome to nginx!</h1>

  1. <p>If you see this page, the nginx web server is successfully installed and

  2. working. Further configuration is required.</p>

  3.  

  4. <p>For online documentation and
    support please refer to

  5. <a href=”http://nginx.org/"&gt;nginx.org&lt;/a&gt;.&lt;br/&gt;

  1. Commercial support is available at

  2. <a href=”http://nginx.com/"&gt;nginx.com&lt;/a&gt;.&lt;/p&gt;

  1.  

  2. <p><em>Thank you for using nginx.</em></p>

  3. </body>

  4. </html>

浏览器访问

图片 1

  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
  1. <html>

  2. <head><title>Golden,s Nginx
    server blog.</title></head>

  3. <body>

  4. Hi,I am golden.My blog address is

  5. <a href=”http://www.cnblogs.com/yinshoucheng-golden"&gt;http://www.cnblogs.com/yinshoucheng-golden&lt;/a&gt;

  1. </body>

  2. </html>

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

图片 2

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
  1.  

  2. #user nobody;

  3. worker_processes 1; # 4 –
    10行main区,nginx主旨效能模块

  4.  

  5. #error_log logs/error.log;

  6. #error_log logs/error.log notice;

  1. #error_log logs/error.log info;
  1.  

  2. #pid logs/nginx.pid;

  3.  

  4.  

  5. events { # 13 – 15 行
    events区,nginx核心效率模块

  6.     worker_connections 1024;

  7. }

  8.  

  9.  

  10. http { # http区,nginx http主题模块

  1.     include mime.types;

  2.     default_type
    application/octet-stream;

  3.  

  4.     #log_format main ‘$remote_addr –
    $remote_user [$time_local] “$request” ‘

  5.     # ‘$status $body_bytes_sent
    “$http_referer” ‘

  6.     # ‘”$http_user_agent” “$http_x_forwarded_for”‘;

  7.  

  8.     #access_log logs/access.log main;

  1.  

  2.     sendfile on;

  3.     #tcp_nopush on;

  4.  

  5.     #keepalive_timeout 0;

  6.     keepalive_timeout 65;

  7.  

  8.     #gzip on;

  9.  

  10.     server { # server标签

  11.         listen 80; # 端口

  12.         server_name localhost; # 域名

  1.  

  2.         #charset koi8-r;

  3.  

  4.         #access_log
    logs/host.access.log main;

  5.  

  6.         location / { # 按照uri举行跳转

  1.             root html;

  2.             index index.html index.htm;
    # 首页

  3.         }

  4.  

  5.         #error_page 404 /404.html;

  1.  

  2.         # redirect server error pages
    to the static page /50x.html

  1.         #

  2.         error_page 500 502 503 504
    /50x.html;

  3.         location = /50x.html {

  4.             root html;

  5.         }

  6.  

  7.         # proxy the PHP scripts to
    Apache listening on 127.0.0.1:80

  8.         #

  9.         #location ~ \.php$ {

  10.         # proxy_pass http://127.0.0.1;

  11.         #}

  12.  

  13.         # pass the PHP scripts to
    FastCGI server listening on 127.0.0.1:9000

  14.         #

  15.         #location ~ \.php$ {

  16.         # root html;

  17.         # fastcgi_pass
    127.0.0.1:9000;

  18.         # fastcgi_index index.php;

  1.         # fastcgi_param
    SCRIPT_FILENAME /scripts$fastcgi_script_name;

  2.         # include fastcgi_params;

  1.         #}

  2.  

  3.         # deny access to .htaccess
    files, if Apache’s document root

  1.         # concurs with nginx’s one
  1.         #

  2.         #location ~ /\.ht {

  3.         # deny all;

  4.         #}

  5.     }

  6.  

  7.  

  8.     # another virtual host using mix of IP-, name-, and port-based
    configuration

  9.     #

  10.     #server {

  11.     # listen 8000;

  12.     # listen somename:8080;

  13.     # server_name somename alias
    another.alias;

  14.  

  15.     # location / {

  16.     # root html;

  17.     # index index.html index.htm;

  1.     # }

  2.     #}

  3.  

  4.  

  5.     # HTTPS server

  6.     #

  7.     #server {

  8.     # listen 443 ssl;

  9.     # server_name localhost;

  10.  

  11.     # ssl_certificate cert.pem;

  1.     # ssl_certificate_key cert.key;
  1.  

  2.     # ssl_session_cache
    shared:SSL:1m;

  3.     # ssl_session_timeout 5m;

  1.  

  2.     # ssl_ciphers HIGH:!aNULL:!MD5;

  1.     # ssl_prefer_server_ciphers
    on;

  2.  

  3.     # location / {

  4.     # root html;

  5.     # index index.html index.htm;

  1.     # }

  2.     #}

  3.  

  4. }

 

  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;

  1.         }

  2.         error_page 500 502 503 504
    /50x.html; #并发对应的http状态码使用50x.html回应

  3.         location = /50x.html {
    #location区块起首,访问50x.html

  4.             root html;
    #点名相应的站点目录为html

  5.         }

  6.     }

  7. }

  8. [root@lnmp conf]# egrep -v “#|^$” nginx.conf.default >nginx.conf

搭建基于域名的虚拟主机

  1. [root@lnmp conf]# cat nginx.conf
  1. worker_processes 1; #worker进程的数码
  1. events { #事件区块开端

  2.     worker_connections 1024;
    #每个worker进程补助的最达累斯萨兰姆接数

  3. } #事件区块起始

  4. http { #http区块开首

  5.     include mime.types;
    #nginx辅助的媒体类型库文件包含

  6.     default_type
    application/octet-stream; #默认的媒体类型

  7.     sendfile on; #拉开高效传输形式

  1.     keepalive_timeout 65; #老是超时
  1.     server {
    #第一个server区块开头,表示一个独立的虚拟主机站点

  2.         listen 80;
    #提供劳务的端口,默认80

  3.         server_name
    www.etiantian.org;
    #提供服务的域名主机名

  4.         location / {
    #首先个location区块先导

  5.             root html/www;
    #站点的根目录,相对于nginx安转目录

  6.             index index.html index.htm;
    #默认的首页文件,多少个用空格分开

  7.         } #率先个localtion区块结果

  1.     }

  2.     server {

  3.         listen 80;

  4.         server_name bbs.etiantian.org;

  1.         location / {

  2.             root html/bbs;

  3.             index index.html index.htm;

  1.         }

  2.     }

  3.  

  4. }

 

  1. [root@lnmp conf]# mkdir
    ../html/{www,bbs} -p

  2. [root@lnmp conf]# tree ../html/

  1. ../html/

  2. ├── 50x.html

  3. ├── bbs

  4. ├── index2.html

  5. ├── index.html

  6. └── www

  7. [root@lnmp conf]# echo “www.etiantian.org” >../html/www/index.html

  1. [root@lnmp conf]# echo “bbs.etiantian.org” >../html/bbs/index.html
  1. [root@lnmp conf]# cat
    ../html/{www,bbs}/index.html

  2. www.etiantian.org

  3. bbs.etiantian.org

  4. [root@lnmp conf]#
    /application/nginx/sbin/nginx -t #自我批评语法

  5. nginx: the configuration file
    /application/nginx-1.6.3/conf/nginx.conf syntax is ok

  6. nginx: configuration file
    /application/nginx-1.6.3/conf/nginx.conf test is successful

  7. [root@lnmp conf]#
    /application/nginx/sbin/nginx -s reload #重启

  8. [root@lnmp conf]# cat /etc/hosts

  1. 127.0.0.1 localhost
    localhost.localdomain localhost4 localhost4.localdomain4

  2. ::1 localhost localhost.localdomain
    localhost6 localhost6.localdomain6

  3.  

  4. 192.168.31.132 server

  5. 192.168.31.133 lamp

  6. 192.168.31.134 lnmp www.etiantian.org
    bbs.etiantian.org

  7. 192.168.31.136 backup

  8.  

  9. [root@lnmp conf]# ping
    www.etiantian.org

  10. PING lnmp (192.168.31.134) 56(84) bytes
    of data.

  11. 64 bytes from lnmp (192.168.31.134):
    icmp_seq=1 ttl=64 time=0.084 ms

  12. 64 bytes from lnmp (192.168.31.134):
    icmp_seq=2 ttl=64 time=0.050 ms

  13. ^Z

  14. [5]+ Stopped ping www.etiantian.org

  1. [root@lnmp conf]# ping
    bbs.etiantian.org

  2. PING lnmp (192.168.31.134) 56(84) bytes
    of data.

  3. 64 bytes from lnmp (192.168.31.134):
    icmp_seq=1 ttl=64 time=0.034 ms

  4. 64 bytes from lnmp (192.168.31.134):
    icmp_seq=2 ttl=64 time=0.050 ms

  5. ^Z

  6. [6]+ Stopped ping bbs.etiantian.org

  1. [root@lnmp conf]# curl
    www.etiantian.org

  2. www.etiantian.org

  3. [root@lnmp conf]# curl
    bbs.etiantian.org

  4. 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
  1. worker_processes 1;

  2. events {

  3.     worker_connections 1024;

  4. }

  5. http {

  6.     include mime.types;

  7.     default_type
    application/octet-stream;

  8.     sendfile on;

  9.     keepalive_timeout 65;

  10.     server {

  11.         listen 8001;

  12.         server_name www.etiantian.org;

  1.         location / {

  2.             root html/www;

  3.             index index.html index.htm;

  1.         }

  2.     }

  3.     server {

  4.         listen 8002;

  5.         server_name www.etiantian.org;

  1.         location / {

  2.             root html/bbs;

  3.             index index.html index.htm;

  1.         }

  2.     }

  3.    server {

  4.         listen 8003;

  5.         server_name www.etiantian.org;

  1.         location / {

  2.             root html/blog;

  3.             index index.html index.htm;

  1.         }

  2.     }

  3.  

  4. }

 

  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

  1.     inet6 ::1/128 scope host

  2.        valid_lft forever
    preferred_lft forever

  3. 2: eth0:
    <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast
    state UP qlen 1000

  4.     link/ether 00:0c:29:03:06:08 brd
    ff:ff:ff:ff:ff:ff

  5.     inet 192.168.31.134/24 brd
    192.168.31.255 scope global eth0

  6.     inet 192.168.31.135/24 scope global
    secondary eth0

  7.     inet6 fe80::20c:29ff:fe03:608/64
    scope link

  8.        valid_lft forever
    preferred_lft forever

  9. [root@lnmp conf]# cat nginx.conf

  1. worker_processes 1;

  2. events {

  3.     worker_connections 1024;

  4. }

  5. http {

  6.     include mime.types;

  7.     default_type
    application/octet-stream;

  8.     sendfile on;

  9.     keepalive_timeout 65;

  10.     server {

  11.         listen 192.168.31.134:80;

  1.         server_name www.etiantian.org;
  1.         location / {

  2.             root html/www;

  3.             index index.html index.htm;

  1.         }

  2.     }

  3.     server {

  4.         listen 192.168.31.135:80;

  1.         server_name www.etiantian.org;
  1.         location / {

  2.             root html/bbs;

  3.             index index.html index.htm;

  1.         }

  2.     }

  3. }

  4. [root@lnmp conf]#
    /application/nginx/sbin/nginx -t

  5. nginx: the configuration file
    /application/nginx-1.6.3/conf/nginx.conf syntax is ok

  6. nginx: configuration file
    /application/nginx-1.6.3/conf/nginx.conf test is successful

  7. [root@lnmp conf]#
    /application/nginx/sbin/nginx -s reload

  8. [root@lnmp conf]# curl
    192.168.31.134

  9. www.etiantian.org

  10. [root@lnmp conf]# curl
    192.168.31.135

  11. 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

  1.     inet6 ::1/128 scope host

  2.        valid_lft forever
    preferred_lft forever

  3. 2: eth0:
    <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast
    state UP qlen 1000

  4.     link/ether 00:0c:29:03:06:08 brd
    ff:ff:ff:ff:ff:ff

  5.     inet 192.168.31.134/24 brd
    192.168.31.255 scope global eth0

  6.     inet6 fe80::20c:29ff:fe03:608/64
    scope link

  7.        valid_lft forever
    preferred_lft forever

     

相关文章