Nginx(tengine )

下载Tengine (Tengine时加强版的Nginx)
http://tengine.taobao.org/download.html
cd usr/local
wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz

将配置好的网路配置发送给各个节点(只需修改ip即可)
scp -r /etc/sysconfig/network-scripts/ifcfg-ens33 192.168.1.7:/etc/sysconfig/network-scripts/ifcfg-ens33
将解压配置好的文件传入各个节点(只需修改ip即可)
scp -r ./tengine-2.3.3 192.168.1.7:/usr/local/
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
安装运行环境
make && make install
./configure --prefix=/usr/local/tengine
make && make install
cd tengine
cd sbin/
./nginx

 


 vim /etc/sysconfig/network-scripts/ifcfg-ens33

service network restart

./nginx
这里 会发现 本地linux可以访问 192.168.1.5 出现页面

但是外面windows无法访问linux的ip地址 需要关闭防火墙

 vim   /etc/resolv.conf

nameserver 192.168.1.1
nameserver 114.114.114.114service network restart这里就可以ping www.baidu.com了
查看防火墙状态命令:systemctl status firewalld.service
   systemctl stop firewalld.service        关闭运行的防火墙(重启后还会开启防火墙)
systemctl disable firewalld.service,开机禁止防火墙服务器
-----------》systemctl enable firewalld.service,开机启动防火墙服务器

 

启动服务

脚本自启动
拷贝附件提供的Nginx启动脚本文件内容到/etc/init.d/nginx这个文件中
目录下如果没有这个文件的话需要手动创建
修改可执行权限
cd /etc/init.d/
chmod 777 nginx

启动服务

service Nginx start 启动服务
ps -ef | grep nginx

service nginx stop 停止 service nginx status 状态 service nginx reload 动态重载配置文件

#!/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="/usr/local/tengine/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/tengine/conf/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
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
 
Nginx配置解析
定义Nginx运行的用户和用户组
user  nobody 当前启动用户 不配置的话当前系统已经登陆的用户 可以配置用户的组
worker_processes  1; 进程个数 可以开启多少个进程 一个nginx可以开启多个线程 一个线程可以开启多个子进程 默认和cpu个数绑定
events {
    worker_connections  1024; 一个worker能够产生多少链接
}
nginx可以产生上万的链接
worker_processes*worker_connections 要小于 cat file-max
------------------------------------------------
cd /proc/sys/fs 当前服务器允许打开文件的个数
 cat file-max
cd /proc/sys/fs
cat file-nr 当前句柄的使用

打开的最多文件描述符

一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致

include       mime.types 引入文件 文件扩展名和映射列表
default_type  application/octet-stream;如果 上面的映射文件没有匹配上 就默认类型为application/octet-stream 需要用户自己去判断 可执行程序 不能在浏览器中打开
sendfile        on; 文件传输异步执行 复制一个文件到另一个文件夹中 较慢 但是 剪切一个文件到另一个文件夹 速度很快 sendfile on=ctrl+x sendfile off =ctrl+c ctrl+v sendfile 不仅能操作网络文件 也能操作本地文件
sendfile 操作网路 也是需要经过操作系统的

 


 sendfile on 开启 可能会导致 接受文件方来不及解析 这时需要关闭sendfile 生产者大于消费者的消费能力

keepalive_timeout  65; 链接超时 
tcp_nopush     on; 网络优化属性 tcp缓存
gzip on; 开启gzip压缩输出
gzip_min_length 1k; 设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于2k的字节数,小于2k可能会越压越大。
server
listen 80; 监听端口 server_name www.mashibing.com mashibing.com; 域名可以有多个,用空格隔开  一个server  对于一个独立的主机
server_name  localhost 内网主机名 外网域名 一个nginx支持多个server
一个网卡可以绑定多个ip 不同的ip可以显示不同的站点(ip可以改成主机名和或者域名) 多个域名指向一台主机

通过nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置

  • 基于ip的虚拟主机, (一块主机绑定多个ip地址)
  • 基于域名的虚拟主机(servername)
  • 基于端口的虚拟主机(listen如果不写ip端口模式)
  • http{
    	server{
    		#表示一个虚拟主机
    	}
    }
  • #charset koi8-r; 字符集编码
映射/虚拟目录
location / {
            root   html; 跟目录下的  html文件(相对当前文件的html文件)
       //root /var/data/ww 这里是绝对路径下的文件路径 index index.html index.htm; //index 下的index.html }
只要是以/开头的都要进入 { }中的规则这个
会匹配到tengine html文件下的
index默认页 indexhtml (当你输入/后啥也不输入 默认访问html文件下的index.html)
service nginx reload 修改了nginx.conf后重新加载文件
yy 复制
p 粘贴

 error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}
location [ = | ~ | ~* | ^~ ] uri { ... }

location URI {} 对当前路径及子路径下的所有对象都生效;

location = URI {} 注意URL最好为具体路径。 精确匹配指定的路径,不包括子路径,因此,只对当前资源生效;

location ~ URI {} location ~* URI {} 模式匹配URI,此处的URI可使用正则表达式,~区分字符大小写,~*不区分字符大小写;

location ^~ URI {} 禁用正则表达式

优先级:= > ^~ > ~|~* > /|/dir/

location配置规则

location 的执行逻辑跟 location 的编辑顺序无关。 矫正:这句话不全对,“普通 location ”的匹配规则是“最大前缀”,因此“普通 location ”的确与 location 编辑顺序无关;

但是“正则 location ”的匹配规则是“顺序匹配,且只要匹配到第一个就停止后面的匹配”;

“普通location ”与“正则 location ”之间的匹配顺序是?先匹配普通 location ,再“考虑”匹配正则 location 。

注意这里的“考虑”是“可能”的意思,也就是说匹配完“普通 location ”后,有的时候需要继续匹配“正则 location ”,有的时候则不需要继续匹配“正则 location ”。两种情况下,不需要继续匹配正则 location :

( 1 )当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普通 location 一旦匹配上,则不需要继续正则匹配;
( 2 )当普通location 恰好严格匹配上,不是最大前缀匹配,则不再继续匹配正则

 

        location /doc/ {
            root /usr/webApp/; 

        }

如果访问的是以/doc/开头 那么nginx会将localtion后面的地址拼接到root路径后面
例如访问/doc/a.html

就是访问 /usr/webApp/doc/a.html


 

posted @ 2022-04-14 22:15  花心大萝卜li  阅读(550)  评论(0)    收藏  举报