内容摘要:

  nginx 特点

  nginx应用场合

  nginx安装

  nginx主配置文件

  nginx基于域名的虚拟主机

  nginx基于域名的端口主机

  nginx基于域名的IP主机

  nginx配置负载均衡

  nginx加入service添加启动

 

nginx 特点

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

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

3、占用资源少。2W并发 开10个线程服务,内存消耗几百M。

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

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

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

7、nginx可以配合动态PHP(FASTCGI接口)

它所具备的其他WWW服务特性如下:

支持基于名字、端口以及IP的多虚拟主机站点:

支持rewrite模块,支持URI重写及正则表达式匹配:

支持http响应速率限制:

支持同一IP地址的并发连接或请求数限制

 

nginx应用场合

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

html,js,css,.flv,jpg,gif等,类似lighttpd。

2、提供动态服务,nginx+fastcgi的方式运行php,jsp。

动态并发:500-1500

3、提供反向代理(proxy)服务,或者称为负载均衡。日PV2000W 以下,并发1万以下,都可以直接用NGINX 做代理。

4、缓存服务,类似SQUID,VARNISH。

 

nginx 虚拟主机

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

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

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

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

 

nginx安装

nginx网址 nginx.org
a、安装PCRE(Perl Compatible Regular Expressions),peri兼容正则表达式,官方站点为www.pcre.org
先查看服务器的版本
cat /etc/redhat-release
uname -m
查看是否有pcre
rpm -qa pcre pcre-devel
安装pcre
yum install pcre pcre-devel -y
 
b、SSL modules require the OpenSSL library
安装openssl
yum install openssl openssl-devel -y
 
c、安装nginx
创建虚拟用户
useradd nginx -s /sbin/nologin -M
解压文件
tar zxcf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --help 列出所有的可选项
./configure --prefix=/mnt/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
echo $? 判断是否成功
0
然后 make 编译安装
make
make install
 
启动nginx
/mnt/nginx-1.6.3/sbin/nginx
检查是否启动
ps -ef |grep nginx| grep -v grep 或者 ss -lntup | grep nginx
 
/mnt/nginx/sbin/nginx -V 来查看这个是如何编译安装的
 
部署一个简单的web站点
cd /mnt/nginx-1.6.3
ls -l | grep -v temp
drwxr-xr-x. 2 root root 4096 12月 9 10:19 conf 配置
drwxr-xr-x. 2 root root 4096 12月 9 10:19 html 默认网站
drwxr-xr-x. 2 root root 4096 12月 9 10:29 logs 错误,访问日志,PID
drwxr-xr-x. 2 root root 4096 12月 9 10:19 sbin 启动命令
cd html
vim index.html 随意修改后,保存
可以发现这个时候,nginx 默认首页已经变化了

 

nginx主配置文件

 

#user nobody;
worker_processes 1;
 
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
 
#pid logs/nginx.pid;
------------------------------------------ Main区,Nginx核心功能模块
 
events {
worker_connections 1024;
}
------------------------------------------ events区,Nginx核心功能模块
 
http {
include mime.types;
default_type application/octet-stream;
 
sendfile on; #高效文件传输
keepalive_timeout 65; #超时时间
 
#一个server标签就是一个虚拟主机
server {
listen 80;#监听的端口
server_name localhost;#域名
#所有的请求,执行的参数 location区块
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
#如果是下面的情况,优先执行下面
location = /50x.html {
root html;
}
}
}

 

nginx基于域名的虚拟主机

最小化配置文件,去掉#和空格

egrep -v "#|^$" nginx.conf.default >nginx.conf
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www; #目录为www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs; #目录为bbs
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
创建www和bbs的目录,分别设置站点
mkdir mnt/nginx/html/{www,bbs} -p
echo "www" > www/index.html
echo "bbs" > bbs/index.html
#检查语法是否OK
/mnt/nginx/sbin/nginx -t
hosts绑定本机IP后,测试成功
[root@localhost html]# curl www.etiantian.org
www
[root@localhost html]# curl bbs.etiantian.org
bbs

  

nginx基于端口的虚拟主机

修改server中的端口即可

server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www; #目录为www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8001;
server_name www.etiantian.org;
location / {
root html/www; #目录为www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

  

nginx基于IP的虚拟主机

用的少,这边不描述

 

 

nginx配置负载均衡

 

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;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream test.miaohr.com {
    	server 118.31.239.163:80;
    	server 118.31.239.163:8080;
    }  

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://test.miaohr.com ;     
        #proxy_set_header  X-Real-IP  $remote_addr;     
        client_max_body_size  100m;         
        }

 

  这边配置负载均衡池

upstream test.miaohr.com {
    	server 118.31.239.163:80;
    	server 118.31.239.163:8080;
    }  
后面写好地址即可
proxy_pass http://test.miaohr.com ;

 

nginx加入service添加启动

nginx性能的tps nginx性能配置_虚拟主机

nginx性能的tps nginx性能配置_nginx_02

1 #!/bin/bash
  2 
  3 # nginx Startup script for the Nginx HTTP Server
  4 
  5 # it is v.0.0.2 version.
  6 
  7 # chkconfig: - 85 15
  8 
  9 # description: Nginx is a high-performance web and proxy server.
 10 
 11 #              It has a lot of features, but it's not for everyone.
 12 
 13 # processname: nginx
 14 
 15 # pidfile: /var/run/nginx.pid
 16 
 17 # config: /usr/local/nginx/conf/nginx.conf
 18 
 19 nginxd=/usr/local/nginx/sbin/nginx
 20 
 21 nginx_config=/usr/local/nginx/conf/nginx.conf
 22 
 23 nginx_pid=/var/run/nginx.pid
 24 
 25 RETVAL=0
 26 
 27 prog="nginx"
 28 
 29 # Source function library.
 30 
 31 . /etc/rc.d/init.d/functions
 32 
 33 # Source networking configuration.
 34 
 35 . /etc/sysconfig/network
 36 
 37 # Check that networking is up.
 38 
 39 [ ${NETWORKING} = "no" ] && exit 0
 40 
 41 [ -x $nginxd ] || exit 0
 42 
 43 # Start nginx daemons functions.
 44 
 45 start() {
 46 
 47 if [ -e $nginx_pid ];then
 48 
 49    echo "nginx already running...."
 50 
 51    exit 1
 52 
 53 fi
 54 
 55    echo -n $"Starting $prog: "
 56 
 57    daemon $nginxd -c ${nginx_config}
 58 
 59    RETVAL=$?
 60 
 61    echo
 62 
 63    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
 64 
 65    return $RETVAL
 66 
 67 }
 68 
 69 # Stop nginx daemons functions.
 70 
 71 stop() {
 72 
 73         echo -n $"Stopping $prog: "
 74 
 75         killproc $nginxd
 76 
 77         RETVAL=$?
 78 
 79         echo
 80 
 81         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
 82 
 83 }
 84 
 85 # reload nginx service functions.
 86 
 87 reload() {
 88 
 89     echo -n $"Reloading $prog: "
 90 
 91     #kill -HUP `cat ${nginx_pid}`
 92 
 93     killproc $nginxd -HUP
 94 
 95     RETVAL=$?
 96 
 97     echo
 98 
 99 }
100 
101 # See how we were called.
102 
103 case "$1" in
104 
105 start)
106 
107         start
108 
109         ;;
110 
111 stop)
112 
113         stop
114 
115         ;;
116 
117 reload)
118 
119         reload
120 
121         ;;
122 
123 restart)
124 
125         stop
126 
127         start
128 
129         ;;
130 
131 status)
132 
133         status $prog
134 
135         RETVAL=$?
136 
137         ;;
138 
139 *)
140 
141         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
142 
143         exit 1
144 
145 esac
146 
147 exit $RETVAL

View Code