首先提权到root用户
apt-get install make build-essential libtool zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev -y
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.18.0.tar.gz #最新版本在http://nginx.org/en/download.html查看
tar zxvf nginx-1.18.0.tar.gz #解压安装包
cd nginx-1.18.0 #进入安装目录
#安装在/usr/local/nginx
./configure \
--prefix=/usr/local/nginx \
--with-openssl-opt=enable-tls1_3 \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream
make && make install
/usr/sbin/groupadd nginx
/usr/sbin/useradd -g nginx nginx
cd /usr/local/nginx/conf/
mv nginx.conf nginx.conf.bak
配置nginx.conf ,将/usr/local/nginx/conf/nginx.conf改为以下内容
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx; #配置用户或者组
worker_processes auto;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535; #与ulimit -n 的值保持一致
events
{
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
use epoll; #高性能网络I/O模型,不用管
worker_connections 65535; #单个进程最大连接数(最大连接数=连接数*进程数),与ulimit -n 的值一致即可
}
http{
server_tokens off; #关闭在错误页面中的nginx版本数字
sendfile on; #进行下载等应用磁盘IO重负载应用,可设置为 off
#autoindex on; #目录列表访问,下载服务器设置,默认不设置
error_log /usr/local/nginx/logs/error.log crit; #记录严重的错误,注意修改路径
log_format access escape=json '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/nginx/logs/access.log access; #记录访问日志,上面为日志访问格式
keepalive_timeout 1800; #长连接超时时间,扫码设置为1800
keepalive_requests 500; #每个长连接最大请求数
limit_conn_zone $binary_remote_addr zone=TotalConnLimitZone:10m ;#设置IP限速
limit_req_zone $binary_remote_addr zone=ConnLimitZone:10m rate=50r/s; #每个 IP 地址每秒最大处理 50 个请求
include mime.types; #调用样式
#include blocksip.conf; #配置禁用访问ip,默认进行关闭,需要自行配置
default_type application/octet-stream;
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,默认这样就行
large_client_header_buffers 4 64k; #客户请求头缓冲大小,默认就行
client_max_body_size 100m; #设定通过nginx上传文件的大小,需要上传文件的注意这个参数
gzip on; #采用数据压缩
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本
gzip_comp_level 4; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型
gzip_vary on;
##缓存cache参数配置
proxy_connect_timeout 5; #与后端程序连接超时时间,单位为秒
proxy_read_timeout 60; #读取后端程序超时时间,扫码业务设置为1800,注意
proxy_send_timeout 5; #向后端发送超时时间
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#缓存到nginx的本地目录
proxy_temp_path /usr/local/nginx/html/nginx_temp; #设置缓存临时目录
proxy_cache_path /usr/local/nginx/html/nginx_temp/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
#设置缓存文件夹;keys_zone缓存空间名; inactive文件1天内没有被访问就删除,max_size缓存占用最大空间为10G
upstream backend
{
server 192.168.18.19:8080; #设置代理地址
server 192.168.18.19:8089; #设置代理地址
keepalive 16; #启动后端长连接
}
server {
listen 9000; #监听端口
server_name hbsm.xinli.com.cn; #监听地址,域名可以有多个,用空格隔开
root html; #根目录
index index.php index.htm index.html; #默认页
#动态请求代理给相应服务器
location / {
#include agent_deny.conf; #屏蔽爬虫攻击,需要外部配置,默认关闭,配置好外部配置打开
limit_req zone=ConnLimitZone burst=100 nodelay; #设置限速200个排队
limit_conn TotalConnLimitZone 100; #限制每个IP只能发起100个并发连接
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect off;
proxy_http_version 1.1; #设置Http协议版本
proxy_pass http://backend; #转向定义服务列表
}
#缓存相应的文件(静态文件)
location ~ \.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
#include agent_deny.conf; #屏蔽爬虫攻击,需要外部配置,默认关闭,配置好外部配置打开
proxy_pass http://backend; #如果没有缓存则通过proxy_pass转向请求
#proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; #强制缓存,部分应用会不让静态页面缓存,此方法可以强制缓存静态页,默认关闭
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one; #设置缓存共享内存区块,也就是keys_zone名称
proxy_cache_valid 200 302 1h; #对不同的HTTP状态码设置不同的缓存时间,h小时,d天数
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 7d; #设置用户本地缓存文件失期时间
}
}
}
#屏蔽带有关键词/getCCHelpDownloadInfo的url
location ~ /getCCHelpDownloadInfo {
return 403;
access_log off; #此类消息不加入日志
}
vi /usr/local/nginx/conf/agent_deny.conf
#禁止URL带有“&url=”的关键词访问
if ($request ~* (&url=|qq.com|kugou.com)) {
return 403;
access_log off; #此类消息不加入日志
}
#禁止Scrapy等工具的抓取,注意是否屏蔽了监控
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
return 403;
access_log off; #此类消息不加入日志
}
#禁止指定UA及UA为空的访问
if ($http_user_agent ~ "COOCAREHTTPULPADAGENT|WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|YandexBot|FlightDeckReports|Linguee Bot|^$" ) {
return 403;
access_log off; #此类消息不加入日志
}
#禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 403;
access_log off; #此类消息不加入日志
}
#修改设置cookies,多层判断示例,nginx不支持嵌套判断
if ($request ~ &version=6.0&) {
set $iftm Y;
}
if ($http_user_agent !~* (Android/6.0/)) {
set $iftm "${iftm}Y";
}
if ($iftm = YY) {
#return 201 'into responseToauth00';
#add_header Set-Cookie 'Key=system';
#access_log off; #此类消息不加入日志
}
然后在nginx.conf的location中加入
include agent_deny.conf;
upstream backend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
upstream backend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
upstream backend {
server server1.linuxany.com;
server server2.linuxany.com;
fair;
}
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
/usr/local/nginx/sbin/nginx -t
cd /usr/local/
chown -R nginx:nginx nginx/
chmod -R 755 nginx/
vi /usr/local/nginx/conf/blocksip.conf
deny 47.88.226.12;
allow all;
/usr/local/nginx/sbin/nginx
ps -ef |grep nginx #查看是否启动
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
vi /etc/init.d/nginx # 编辑脚本
脚本内容
#!/bin/bash
#nx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
chmod a+x /etc/init.d/nginx #赋予脚本执行权限
ubuntu加入系统服务
update-rc.d nginx defaults
centos加入系统服务
chkconfig --add nginx
chkconfig nginx on
service nginx start #启动nginx
service nginx stop #停止nginx
service nginx restart #停止nginx
浏览器访问服务器ip+端口查看是否能访问网页,刷新看是否能切换到不同网页。
200 正常
304 已经缓存,不用去服务器取
499 用户主动断开,常见用户获取二维码后关闭了客户端
504 nginx把连接主动断开了,查看设置的超时时间