nginx 安装配置+清缓存模块安装

本文介绍了如何安装配置Nginx,包括下载软件包、编译安装、内核参数优化、配置范例站点及启动关闭流程。通过内核参数优化和配置调整,提升Nginx在并发和负载能力上的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经过一段时间的使用,发现 nginx 在并发与负载能力方面确实优于 apache,现在已经将大部分站点从 apache 转到了 nginx 了。以下是 nginx 的一些简单的安装配置。

下载软件包

# mkdir /usr/local/src/tarbag
# mkdir /usr/local/src/software
# cd /usr/local/src/tarbag/
Nginx
# wget http://www.nginx.org/download/nginx-1.0.6.tar.gz
Nginx cache purge 模块(可选)
# wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz

编译安装

# cd /usr/local/src/tarbag/
# tar -xzvf nginx-1.0.6.tar.gz -C /usr/local/src/software
# tar -xzvf ngx_cache_purge-1.3.tar.gz -C /usr/local/src/software
# cd /usr/local/src/software/
# ./configure \ –prefix=/usr/local/nginx-1.0.6 \ # 安装路径
5
WWW.TTLSA.COM 网站作品,作者:凉白开,漠北 由 DONAN 整理,QQ:305765814 QQ 群:6690706with-http_stub_status_module \ # 启用 nginx 状态模块
–with-http_ssl_module \ # 启用 SSL 模块
–with-http_realip_module \ # 启用 realip 模块(将用户 IP 转发给后端服务器)
–add-module=../ngx_cache_purge-1.3 # 添加缓存清除扩展模块
# make
# make install

内核参数优化

# vi sysctl.conf 增加以下配置
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 1800
net.ipv4.ip_conntrack_max = 16777216 # 如果使用默认参数,容易出现网络丢包
net.ipv4.netfilter.ip_conntrack_max = 16777216# 如果使用默认参数,容易出现网络丢包
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024 65535

配置生效
#sysctl –p
修改 iptables 启动脚本,在 star()函数里面加上
#vi /etc/init.d/iptables
/sbin/sysctl -p

配置范例站点站点

序号
域名
目录
1
www.xiaobian.com
/www/html/www.xiaobian.com
2
bbs.xiaobian.com
/www/html/bbs.xiaobian.com

修改nginx配置文件

# vi nginx.conf
user nobody nobody; # 运行 nginx 的所属组和所有者
worker_processes 2; # 开启两个 nginx 工作进程,一般几个 CPU 核心就写几
error_log logs/error.log notice; # 错误日志路径
pid logs/nginx.pid; # pid 路径
events {
worker_connections 1024; # 一个进程能同时处理 1024 个请求
}
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;
keepalive_timeout 65; # keepalive 超市时间
# 开始配置一个域名,一个 server 配置段一般对应一个域名
server {
listen 80; #
# 在本机所有 ip 上监听 80,也可以写为 192.168.1.202:80,这样的话,就只监听 192.168.1.202 上的 80 口
server_name www.xiaobian.com; # 域名
root /www/html/www.xiaobian.com; # 站点根目录(程序目录)
index index.html index.htm; # 索引文件
location / { # 可以有多个 location
root /www/html/www.xiaobian.com; # 站点根目录(程序目录)
}
error_page 500 502 503 504 /50x.html;
# 定义错误页面,如果是 500 错误,则把站点根目录下的 50x.html 返回给用户
location = /50x.html {
root /www/html/www.xiaobian.com;
} }# 开始配置站点 bbs.xiaobian.com
server {
listen 80;
server_name bbs.xiaobian.com;
root /www/html/bbs.xiaobian.com;
index index.html index.htm; # 索引文件
location / {
root /www/html/bbs.xiaobian.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www/html/bbs.xiaobian.com;
} }}

Nginx 启动关闭

# /usr/local/nginx-1.0.6/sbin/nginx //启动 nginx
# /usr/local/nginx-1.0.6/sbin/nginx –t //测试 nginx 配置文件的准确性
# /usr/local/nginx-1.0.6/sbin/nginx –s reload //重载 nginx
# /usr/local/nginx-1.0.6/sbin/nginx –s stop //关闭 nginx

测试

创建测试站点

# mkdir –p /www/html/www.xiaobian.com
# mkdir –p /www/html/bbs.xiaobian.com
# echo “www.heytool.com” > /www/html/www.xiaobian.com/index.html
# echo “bbs.heytool.com” > /www/html/bbs.xiaobian.com/index.html

启动nginx

# /usr/local/nginx-1.0.6/sbin/nginx –t //看到 ok 和 successful,说明配置文件没问题
nginx: the configuration file /usr/local/ nginx-1.0.6/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/ nginx-1.0.6/conf/nginx.conf test is successful
# /usr/local/nginx-1.0.6/sbin/nginx

绑定hosts,测试
把两个域名指向 192.168.1.202
192.168.1.202 www.xiaobian.com
192.168.1.202 bbs.xiaobian.com

ok!!!完毕
游览器输入域名就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值