1. 前提准备
-
需要安装gcc的环境。
yum install gcc-c++
-
安装第三方的开发包
-
PCRE
yum install -y pcre pcre-devel
-
zlib
yum install -y zlib zlib-devel
-
openssl
yum install -y openssl openssl-devel
-
-
可以执行以下命令, 一次性安装
yum install gcc openssl-devel pcre-devel zlib-devel -y
2. 安装tengine
-
解压
-
进入tengine目录下执行:
# /usr/tengine-2.1.0/ 为安装路径 ./configure --prefix=/usr/tengine-2.1.0/
-
安装
make && make install
3. 运行
-
启动tengine
进入tengine的sbin目录
-
启动tengine
./nginx
-
访问
http://192.168.152.100/
-
重启tengine
./nginx -s reload
-
关闭tengine
./nginx -s stop
4. 设置nginx服务
-
进入: cd /etc/init.d 目录, 创建nginx文件
vi /nginx
-
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="/opt/tengine-2.1.0/sbin/nginx" prog=$(basename $nginx) # 修改为自己的路径 NGINX_CONF_FILE="/opt/tengine-2.1.0/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
-
此时可以在任意目录下执行以下操作
service nginx start service nginx stop service nginx reload
三. 配置文件讲解
1. 配置文件
#user nobody;
# 电脑有几个CPU, 这里就写几, 或者是CPU数的2倍
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# work连接数
worker_connections 1024;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
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;
# 多久之后才断开连接, 防止网络中频繁的进行三次握手
# 实验中设置为0, 生产中再开启
keepalive_timeout 0;
# keepalive_timeout 65;
# 是否开启压缩, 节省I/O
#gzip on;
server {
# 监听80端口
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root: 指访问目录(html表示nginx的html目录)
# 一个server中只能设置一个root
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2. 内容分析
1. work连接数设置
-
配置文件
events { # work连接数 worker_connections 1024; }
-
这里的连接数可以设置10000级别, 但是如果只修改这里,并不会生效, 因为内核默认最多只会分配1024
ulimit -a
-
修改内核参数:
ulimit -$Hn 2048
2. location
-
location格式
location / { # root: 指访问目录(html表示nginx的html目录) # 一个server中只能设置一个root root html; index index.html index.htm; }
-
在html目录下创建
abc/test.html
3. location匹配顺序:
-
形式
# 绝对匹配 location = / { [ configuration A ] } # 最大前缀匹配 location / { [ configuration B ] } # location /documents/ { [ configuration C ] } # 不会阻断正则 location ^~ /images/ { [ configuration D ] } # 正则 location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] }
-
先普通
- 顺序无关
- 最大前缀
- 匹配规则简单
-
打断
- ^~
- 完全匹配
-
正则
- 不完全匹配
- 正则特殊性: 一条URI可以和多条location匹配上
- 顺序有关
- 先匹配, 先应用, 即时退出匹配
4. 反向代理
-
例子
location /ooxx { proxy_pass https://www.baidu.com/; }
-
但是此时如果搜索内容又会报错:
-
内容是以/s开头的, 所以会和之前的内容匹配, 不会匹配到ooxx. 所以需要在nginx中重新配置一个以/s开头的反向代理配置
末尾加不加/:
如果proxy_pass 后面的url没有"/", 则会传递location中的内容; 否则不传递
5. 反向代理池