nginx的安装及使用

来源:互联网 发布:卖家多久查出淘宝联盟 编辑:程序博客网 时间:2024/06/04 18:04
阻塞调用:事件没有准备好,那就只能等了,等事件准备好了,你再继续吧。


阻塞调用会进入内核等待,cpu就会让出去给别人用了,对单线程的worker来说,显然不合适,当网络事件越多时,大家都在等待呢,cpu空闲下来没人用,cpu利用率自然上不去了,更别谈高并发了。


异步非阻塞:异步非阻塞的事件处理机制,具体到系统调用就是像select/poll/epoll/kqueue这样的系统调用。
它们提供了一种机制,让你可以同时监控多个事件,调用他们是阻塞的,但可以设置超时时间,在超时时间之内,如果有事件准备好了,就返回。

Wget http://nginx.org/download/nginx-1.10.0.tar.gz

tar -zxvf nginx.tar.gz
./configure


linux 安装gcc,gcc-c++
yum -y install gcc gcc-c++ autoconf automake


安装pcre
yum -y install pcre pcre-devel


安装zlib
yum -y install zlib zlib-devel


make


make install 


启动nginx 服务器
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf


停止nginx服务器
1.从容停止
kill -QUIT 15369


2.快速停止
kill -TERM 15417
kill -INT 15417


3.强制停止
pkill -9 nginx


4.验证配置文件的正确性
./nginx -t
./nginx -t -c /usr/local/nginx/conf/nginx.conf


5.nginx 重启
./nginx -s reload(进入到所在目录)


kill -HUP 15446


6.USR1:切换日志文件
  USR2:平滑升级可执行进程
  WINCH:从容关闭工作进程(kill -WINCH 2255)


7.查看版本
./nginx -V




#设置用户
#user nobody


#工作衍生进程数(等于cpu的核数或2倍的核数)
worker_processes 6;


#设置pid存放的路径
#pid logs/nginx.pid;


//最大连接数
events {
    worker_connections  1024;
}


#开启gzip 开启(用户访问的是压缩过的文件(原来的30%))
#gzip on 


#设置字符编码
charset koi8-r;
charset gb2312;




nginx配置文件的抽象(实例)
user nobody;
worker_processes 4;
events{
worker_connections 1024;
}
http{
server{
listen 192.168.1.7:80;
server_name 192.168.1.7;
access_log logs/server1.access.log.combined;
location /
{
index index.html index.htm;
root html/server1;
}
}


server{
listen 192.168.1.8:80;
  server_name 192.168.1.17;
access_log logs/server2.access.log.combined;
location /
{
index index.html index.htm;
root html/server2;
}
}


}




设置主机的ip地址和子网掩码
ifconfig eth1 192.168.1.10 netmask 255.255.255.0


设置虚拟主机(子服务器)的ip地址和广播地址和子网掩码
ifconfig eth1:1 192.168.1.7 broadcast 192.168.1.255 netmask 255.255.255.0




虚拟主机的配置:在配置好了ip地址之后,我们需要将对应的ip地址与对应的虚拟主机建立联系




log_format指令是用来设置nginx服务器的日志文件的记录格式


log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
remote_addr : ip地址
remote_user : 用户
request     : 请求的网址
status       : 状态
body_bytes_sent : 传送的字节数
http_referer : 原页面
http_user_agent : 浏览器(客户端)
http_x_forwarded_for:类似ip


//修改nginx默认的配置文件
vi /usr/local/nginx/conf/nginx.conf


access_log 存储路径


/*****************nginx 实现负载均衡*********************/
   user nobody;
   worker_processes 4;
   events{
          worker_connections 1024;
   }
   http{
           upstream myproject{
ip_hash;  
                   server 115.239.210.27;
                   server 180.96.12.1;
                  server 42.156.140.7;
                  server 140.205.230.49;
                  server 122.225.67.253;
          }
          server{
                  listen 8080;
                  location /
                  {
                          proxy_pass http://myproject;
                  }
          }
  
  
  }
 
/*****************nginx 实现负载均衡*********************/

1 0
原创粉丝点击