目录
六、配置tengine.service使用systemctl启动、停止、开机启动
一、Nginx能干什么
Nginx是一个高性能的开源反向代理服务器、负载均衡器、Web服务器和缓存服务器。它被广泛用于构建高性能的、可扩展的Web应用程序和服务。
本博文主要讲述如何安装Tengine,能安装Tengine就能安装原版Nginx,他们原理是一样的。
二、Tengine - 淘宝版Nginx安装
Tengine是由淘宝发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝,天猫,优酷,全球速卖通,Lazada,阿里云等得到了很好的检验 --- 这是官方原话。
官网地址:https://tengine.taobao.org/
1、下载tengine
自行到官网下载 tengine,或者使用以下命令下载:
wget https://tengine.taobao.org/download/tengine-3.0.0.tar.gz
我下载的是tengine-3.0.0.tar.gz
2、安装tengine前安装好c语言编译工具
yum install gcc openssl-devel pcre-devel zlib-devel -y
3、解压tengine
tar -zxvf tengine-3.0.0.tar.gz
4、安装tengine
进行tengine目录(cd tengine-3.0.0)
(1)配置软件安装的路径
./configure --prefix=/usr/local/tengine --add-module=./modules/ngx_http_upstream_check_module
(2)编译和安装软件
make && make install
5、启动
执行以下命令启动Nginx
/usr/local/tengine/sbin/nginx
通过进程查看Nginx是否启动成功
ps -ef | grep nginx
6、停止
执行以下命令停止Nginx
/usr/local/tengine/sbin/nginx -s stop
或者使用有序停止
/usr/local/tengine/sbin/nginx -s quit
通过进程查看Nginx是否停止成功
ps -ef | grep nginx
三、默认配置文件解析
打开 /usr/local/tengine/conf/nginx.conf文件可以看到如下内容:
vi /usr/local/tengine/conf/nginx.conf
## user定义Nginx运行的用户和用户组
#user nobody;
## worker_processes配置Nginx运行的进程数,建议设置为等于CPU总核心数。
worker_processes 1;
## error_log配置Nginx全局错误日志
## 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
## 进程文件
#pid logs/nginx.pid;
events {
## 参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
## epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型
## 如果跑在FreeBSD上面,就用kqueue模型。
## use epoll;
## 单个进程最大连接数。最大值跟系统最大文件句柄数有关。
## 并发总数是 worker_processes 和 worker_connections 的乘积。
## 即 max_clients = worker_processes * worker_connections
worker_connections 1024;
}
http {
## 文件扩展名与文件类型映射表
include mime.types;
## 默认文件类型。如果您的文件类型与mime.types没有匹配上,则使用他。
default_type application/octet-stream;
## charset utf-8; #默认编码
## client_header_buffer_size 32k; #上传文件大小限制
#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;
#access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
## 是否开启高效文件传输模式
sendfile on;
## 优化tcp数据传输,仅在sendfile开启时有效。批处理(相于一个buffer缓存满再发)
#tcp_nopush on;
## autoindex on;#开启目录列表访问,合适下载服务器,默认关闭。
## 长连接超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
## 是否开启gzip压缩输出
#gzip on;
## 默认虚拟主机,一个server就是一个主机,可以复制多个。
server {
## http监听端口
listen 80;
## http域名,可以有多个,用空格隔开
server_name localhost;
## server_name test1.qifu.com test2.qifu.com;
## 编码集
#charset koi8-r;
## 日志相关
#access_log logs/host.access.log main;
#access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
## 虚拟目录,这里定义路径“/”的规则。
location / {
## 根目录,是里是ningx下的html目录(是相对路径)。可以是绝对路径。
## 一个server下如个多个root冲突可以使用alias
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
# 扩展名为php文件请求将被代理到http://127.0.0.1
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# 扩展名为php文件请求配置
#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;
#}
# pass the Dubbo rpc to Dubbo provider server listening on 127.0.0.1:20880
# dubbo目录配置
#location /dubbo {
# dubbo_pass_all_headers on;
# dubbo_pass_set args $args;
# dubbo_pass_set uri $uri;
# dubbo_pass_set method $request_method;
#
# dubbo_pass org.apache.dubbo.samples.tengine.DemoService 0.0.0 tengineDubbo dubbo_backend;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# 禁止访问所有扩展名为.ht的文件
#location ~ /\.ht {
# deny all;
#}
}
# upstream for Dubbo rpc to Dubbo provider server listening on 127.0.0.1:20880
# 负载均衡服务器
#upstream dubbo_backend {
# multi 1;
# server 127.0.0.1:20880;
#}
# another virtual host using mix of IP-, name-, and port-based configuration
## 另一个虚拟主机,指定了8000端口
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
## 默认https虚拟主机(默认未开启)。
#server {
## https监听端口
# listen 443 ssl;
## https域名,可以有多个,用空格隔开
# server_name localhost;
## 用于https的数字证书
# ssl_certificate cert.pem;
## 用于https的私钥
# 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;
# }
#}
}
四、IP访问控制
location {
## 允许IP
allow all;
allow 192.168.1.0/24;
allow 192.168.0.0/16;
allow 192.0.0.0/8;
## deny IP /IP段
## 禁止IP
# deyn all;
deny 192.168.1.109;
}
注意先后顺序,我测试时发现allow放在后面不生效。
五、Nginx基本的访问状态监控
location /basic_status {
stub_status on;
}
basic_status可自己定义。配置好在浏览器打开“http://nginx-ip:port/basic_status”就可以查看您Nginx的基本状态了。
注意:nginx-ip:port是您安装nginx服务的地址和nginx端口。
六、配置tengine.service使用systemctl启动、停止、开机启动
1、创建tengine.service文件
vi /usr/lib/systemd/system/tengine.service
[Unit]
Description=Tengine Nginx Web Server
After=network.target
[Service]
Type=forking
#PIDFile=/opt/nginx/tengine.pid
ExecStartPre=/usr/local/tengine/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/tengine/sbin/nginx
ExecReload=/usr/local/tengine/sbin/nginx -s reload
ExecStop=/usr/local/tengine/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、重新加载
systemctl daemon-reload
3、使用systemclt 启动停止等
systemctl start tengine
systemctl stop tengine
systemctl reload tengine
systemctl enable tengine
七、扩展-文件句柄数
1、查看Linux系统可以打开的文件句柄数
查看当前值
cat /proc/sys/fs/file-nr
最大值
cat /proc/sys/fs/file-max
并发连接总数应小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内。所以worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置。
注意:打开文件句柄数量包含了打开SOCKET数量(如mysql连接、redis连接等)
2、系统限制
查看系统限制 ulimit -a
修改句柄数:ulimit -SHn 65535
八、扩展-防火墙
如果您是为了测试,又不会配置防火墙,可以临时禁用防炎墙
systemctl stop firewalld
sudo firewall-cmd --state
也可通过下面方式来开放某一个特定端口,如配置防火墙开放http 8080
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports
或者通过下面方式来开放某一特定服务,如配置防火墙开放http 80
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload
sudo firewall-cmd --list-services