nginx的安装、部署、配置

##环境部署:

[root@miner_k ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)

[root@miner_k ~]# yum -y install pcre-devel
[root@miner_k ~]# yum -y groupinstall  "Development tools"
[root@miner_k ~]# yum -y install openssl-devel

下载资源、解压和安装

[root@miner_k ~]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@miner_k ~]# tar -xvf nginx-1.12.1.tar.gz
[root@miner_k ~]# cd nginx-1.12.1
[root@miner_k ~]# groupadd -r -g 101 nginx
[root@miner_k ~]# useradd -r -g 101 -u 101 nginx
[root@miner_k ~]#./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre

[root@miner_k nginx-1.12.1]# make  && make install

nginx的启动

nginx的启动脚本

[root@miner_k ~]# vim  /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
# 需要修改,设置nginx的的启动脚本位置
nginx="/usr/local/nginx/nginx"
prog=$(basename $nginx)

# 需要修改,设置nginx的pid位置 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/nginx.lock
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

设置启动脚本的权限

[root@miner_k ~]# chmod +x /etc/init.d/nginx

启动服务

[root@miner_k ~]# service nginx start
[root@miner_k ~]# chkconfig --add nginx
[root@miner_k ~]# chkconfig nginx on

nginx的配置

配置文件:

[root@miner_k ~]# cd /etc/nginx/
[root@miner_k nginx]# ls
fastcgi.conf         fastcgi.conf.default	 #使用FastCGI连接时的配置
fastcgi_params		fastcgi_params.default  
koi-utf			koi-win
scgi_params 	scgi_params.default  #CGI的连接方式配置
mime.types          mime.types.default    #多用途互联网邮件扩展(识别非文本文档)
nginx.conf		nginx.conf.default		  #nginx的主配置文件
uwsgi_params	uwsgi_params.default      #和python连接时的配置
win-utf
                             

修改配置文件:
[root@miner_k ~]# vim /etc/nginx/nginx.conf

#user  nobody;		#设置启动程序的用户
worker_processes  1;	#worker线程的个数,根据CPU的个数来确定,如果负载以CPU密集型(消耗CPU为主)应用为主,如SSL或压缩应用,则worker数应与CPU数相同;如果负载以IO密集型为主,如响应大量内容给客户端,则worker数应该为CPU个数的1.5或2倍。

#error_log  logs/error.log;      #错误日志的存放位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;    #PID文件的存放位置

#上面这几个都是用“#”注释,表示使用定制时指定的文件。

设置事件驱动(events)有关的配置

#设置事件驱动中每一个worker的配置
events {
    worker_connections  1024;   每一个worker的连接数据是1024
}

###设置http有关的配置

http {
    include       mime.types;     #设置包含的文件
    default_type  application/octet-stream;    #设置默认支持的类型

    sendfile        on;         #内核直接封装,不经过用户态
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;    #保持长连接

    #gzip  on;        #是否压缩
	
	#server{} 设置虚拟主机
	
}

server字段设置虚拟主机
server {
	        listen       80;   #设置端口
	        server_name  localhost;   #设置虚拟主机
	
	        #charset koi8-r;
	
	        #access_log  logs/host.access.log  main;
	
	        location / {
	            root   html;
	            index  index.html index.htm;
	        }
	  }      
基于端口
server {
        listen       80;
        location / {
            root   /web1;
            index  index.html index.htm;
        }
    }


server {
        listen       8080;
        #listen       somename:8080;
        #server_name  somename  alias  another.alias;

        location / {
            root   /web2;
            index  index.html index.htm;
        }
    }

基于IP
server {
        listen       192.168.2.10:80;
        location / {
            root   /web1;
            index  index.html index.htm;
        }
    }


server {
        listen       192.168.2.11:80;
        #listen       somename:8080;
        #server_name  somename  alias  another.alias;

        location / {
            root   /web2;
            index  index.html index.htm;
        }
    }
基于域名
server {
        listen       80;
        server_name  www.miner_k1.com
        location / {
            root   /web1;
            index  index.html index.htm;
        }
    }


server {
        listen       80;
        #listen       somename:8080;
        server_name  www.miner_k2.com;

        location / {
            root   /web2;
            index  index.html index.htm;
        }
    }
设置默认主机
server {
    listen      80 default_server;
    ...
}


####location{}代码块

location的参数有两类:第一类是前缀字符串;第二类是正则表达式

前缀字符串

如果URL匹配前缀字符串,必须是以这些前缀字符串开始

location /some/path/ {
    ...
}

此处的含义是URI如果以**/some/path/开头则匹配并执行下面的动作。如果请求的URI是/my-site/some/path/index.html不会匹配对应的规则。因为该字段中的“/some/path**”不是最开始的字符串

location 后面的设置需要和请求的URI相匹配,如果匹配会加到root的目录之后。如果一个URI匹配多个location,那么nginx加载时选择前缀最长的匹配。

实例1:

server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data;
    }
}

资源的操作过程:如果请求的是http://localhost/会匹配到第一个,到/data/www的目录下找对应的资源;如果是http://localhost/images/example.html, 会匹配到第二个location,获取/root/data/images/example.html该资源如果没有回返回404;如果是http://localhost/test/a.html ,会匹配到第一个location,获取/data/www/test/a.html资源,如果没有该资源就会返回404.

正则表达式

location [ = | ~ | ~* | ^~ ] URI {}

location [op] URI {
proxy_pass http://172.16.100.11/;
}
其中 op 包括:
= 路径精确匹配,只包含当前路径,不包含其它任何路径。
^~ 不做正则表达式元字符匹配,做逐个字符搜索匹配
~* 不区分大小写,元字符匹配
~ 区分大小写,元字符匹配

实例

		location = /index.html {
            root   /web/eql;
            index  index.html index.htm;
            #【config A】
        }

        location / {
            root   /web;
            index  index.html index.htm;
            #【config B】
        }
        location /doc/ {
            root   /web;
            index  index.html index.htm;
            #【config C】
        }
        location ^~ /images/ {
            root   /web;
            index  index.html index.htm;
            #【config D】
        }
        location ~* \.jpg$ {
            root   /web/jpg/;
            #index  index.html index.htm;
            #【config E】
        }

测试:

curl localhost/    				---> B
curl  localhost/index.html   	---> A
curl localhost/doc  	 		---> C
curl localhost/images 	 		---> D
curl localhost/1.jpg	 		---> E
curl localhost/doc/1.jpg 		---> E
curl localhost/images/1.jpg 	---> D

访问控制

基于IP的访问控制
server {
        listen 12345;
        deny   192.168.1.2;
        allow  192.168.1.1/24;
        deny   all;
    }
 
通过身份认证进行访问控制

设置登录nginx的账号和密码

[root@miner_k nginx]# yum -y install httpd-tools
[root@miner_k nginx]# htpasswd -c -m /etc/nginx/.users user1
New password: 
Re-type new password: 
Adding password for user user1

[root@miner_k nginx]# htpasswd -c -m /etc/nginx/.users user2
New password: 
Re-type new password: 
Adding password for user user2

针对某一目录的访问做限制

location /status {                                       
    auth_basic           “Administrator’s Area”;
    auth_basic_user_file /etc/nginx/.users; 
}

针对所有的目录做限制,开发某一目录为公共区域

server {
    ...
    auth_basic           "Administrator’s Area";
    auth_basic_user_file /etc/nginx/.users;

    location /public/ {
        auth_basic off;
    }
}
IP地址和身份认证的结合使用

使用指令satisfy控制IP和身份认证的限制,如果使用all,这两个条件都满足才能访问,如果使用any,表示有一个条件满足,就能访问。

location /status {
    ...
    satisfy all;    

    deny  192.168.1.2;
    allow 192.168.1.1/24;
    allow 127.0.0.1;
    deny  all;

    auth_basic           "Administrator’s Area";
    auth_basic_user_file conf/htpasswd;
}

记录和监控

实时活动检测
location /status {
            stub_status on;
        }

语法: stub_status on
默认值: None
作用域: location

创建一个 location 区域启用 stub_status

“stub status” 模块返回的状态信息跟 mathopd’s 的状态信息很相似. 返回的状态信息如下:

Active connections: 291

server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
active connections

– 对后端发起的活动连接数

server accepts handled requests

– nginx 总共处理了 16630948 个连接, 成功创建 16630948 次握手 (证明中间没有失败的), 总共处理了 31070465 个请求 (平均每次握手处理了 1.8个数据请求)

reading

– nginx 读取到客户端的Header信息数

writing

– nginx 返回给客户端的Header信息数

waiting

– 开启 keep-alive 的情况下,这个值等于

active - (reading + writing)

,意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接

显示文件列表

ngx_http_autoindex_module只在 ngx_http_index_module模块未找到索引文件时发出请求.

location  /  {
	autoindex  on;
}

FastCGI配置

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        
 location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;   #跳转到fpm的对应地址
     fastcgi_index  index.php;
	 fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;         #fastCGI的参数
     include        fastcgi_params;   #FastCGI的参数配置文档
 }
[root@miner_k nginx]# vim fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;               #CGI接口的版本
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;  #nginx的版本

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

参考文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值