Nginx配置安装及模块介绍

现在比较流行的WEB组合LNMP(LINUX+NGINX+MYSQL+PHP)或者LAMP(LINUX+NGINX+MYSQL+PHP)

nginx   俄罗斯人开发的性能高,开源,wwwweb服务软件

官方网站:http://nginx.org/

 

特点:

  1.     静态小文件 高并发
  2.     配置简单,灵活,经量
  3.     占用资源少,2w并发,几十个线程,内存消耗几百M
  4.     功能种类多,(web,cache,proxy)
  5.     支持epoll模型,所以nginx支持高并发(apache是select模型)
  6.     利用nginx可以对IP限制
  7.     nginx配合动态PHP服务

 NGINX 与 apache对比

    nginx使用的是epoll模型(异步的)

    apache使用的select模型 (同步的)

安装nginx:

  环境的准备:

安装PCRE(perl兼容正则表达)rewrie模块使用(伪静态等)

       yum install pcre pcre-devel -y

  安装OPEN SSL 加密的

 yum install openssl-devel -y

  nginx 安装配置

创建用户www

useradd www -s /sbin/nologin  -M

 创建软件包现在目录,下载软件,解压缩,进入软件目录

 mkdir /home/daxian/tools/ -p
cd /home/daxian/tools/
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz 
cd nginx-1.6.3

配置编译

./configure --user=www --group=www --with-http_ssl_module  --with-http_stub_status_module  --prefix=/application/nginx-1.6.3/

make  && make install

#如果提示 --with-openssl=<PATH>说明这个没装
yum install openssl openssl-devel -y

ln -s /application/nginx-1.6.3/ /application/nginx

#启动
/applicaiton/nginx/sbin/nginx

测试

netstat -lntup|grep nginx

lsof -i:80

192.168.70.125(访问自己IP地址) 

 

 成功!!

 

配置文件管理

配置文件(把井号,跟空行去掉)

egrep  -v "^$|#" nginx.conf.default >nginx.conf

worker_processes  1;        #主区块 nginx核心功能区块
#对应的模块在官网模块Core functionality下面找 events { #events区块,nginx核心功能模块 worker_connections
1024; } http { #nginx http核心模块 include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { # 一个server表示一个网站,只要要有一个端口跟域名 listen 80; #监听端口 server_name localhost; #域名 location / { #默认寻找路径 root html; #程序目录 index index.html index.htm; #首页显示 } error_page 500 502 503 504 /50x.html; location = /50x.html { #=号优先匹配 root html; } } }

 

基于域名的配置

修改 server_name   www.daxian.com

重启nginx

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

修改主页文件

vim /appication/nginx/html/index.html

<html>
  <head>
     <title>daxianer</title>
  </head>
  <body>
    daxianer HELLO !!
  </body>
</html>

要在windows host 做解析

192.168.70.125 www.daxian.com

访问域名  www.daxian.com

 

 

常用模块

    ngx_http_core_module   包括一些核心的http参数配置,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  根据定义的key限制nginx请求过程的速率

  ngx_http_limit_log_module  访问日志模块,指定格式记录nginx客户访问日志等信息

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

   ngx_http_ssl_module       ssl模块,用于加密的http连接

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

 

=============================配置文件规范=============

在conf里面创建extra 目录

mkdir -p /application/nginx/conf/extra

修改配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include extra/www.conf;      
    include extra/bbs.conf;      
    include extra/blog.conf;      
  }

 

cd extra

touch {www,blog,bbs}.conf

vim www.conf(只需要更改server标签以及location下面的 文件目录,以此类推)

 server {
        listen       80;
        server_name  www.daxian.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
      }

 

cd /applicaiton/nginx/html/

mkdir -p {www,blog,bbs}

cd www

创建index.html

随便往里写点什么东西

(以此类推)

 重启nginx

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

访问域名

www.daxian.com

blog.daxian.com

bbs.daxian.com

 

。。。。

 

posted @ 2018-01-18 16:48  大仙儿没溜儿  阅读(511)  评论(0编辑  收藏  举报