转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80077664
1、环境
操作系统: CentOS、 RedHat
IP 地址: 192.168.209.121
2、下载软件包
yum install -y gcc gcc-c++ autoconf wget
yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype*
yum install pcre*
yum install openssl*
cd /usr/local/src
Nginx:
wget http://www.nginx.org/download/nginx-1.9.3.tar.gz
Nginx cache purge 模块(可选):
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
3、编辑安装
tar -zxvf nginx-1.9.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz
cd nginx-1.9.3
./configure --prefix=/usr/local/nginx-1.9.3 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=/usr/local/src/ngx_cache_purge-2.3
make
make install
4、内核参数优化
# vim /etc/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()函数里面加上
# vim /etc/init.d/iptables
/sbin/sysctl -p
5、配置范例站点站点
序号域名
目录
1
www.heytool.com
/www/html/www.heytool.com
2
bbs.heytool.com
/www/html/bbs.heytool.com
6、修改nginx 配置文件
vim 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.209.121:80,这样的话,就只监听 192.168.209.121 上的 80 口
server_name www.heytool.com; # 域名
root /www/html/www.heytool.com; # 站点根目录(程序目录)
index index.html index.htm; # 索引文件
location / { # 可以有多个 location
root /www/html/www.heytool.com; # 站点根目录(程序目录)
}
error_page 500 502 503 504 /50x.html;
# 定义错误页面,如果是 500 错误,则把站点根目录下的 50x.html 返回给用户
location = /50x.html {
root /www/html/www.heytool.com;
}
}
#开始配置站点 bbs.heytool.com
server {
listen 80;
server_name bbs.heytool.com;
root /www/html/bbs.heytool.com;
index index.html index.htm; # 索引文件
location / {
root /www/html/bbs.heytool.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www/html/bbs.heytool.com;
}
}
}
7、Nginx 启动关闭
# /usr/local/nginx-1.9.3/sbin/nginx //启动 nginx
# /usr/local/nginx-1.9.3/sbin/nginx –t //测试 nginx 配置文件的准确性
# /usr/local/nginx-1.9.3/sbin/nginx –s reload //重载 nginx
# /usr/local/nginx-1.9.3/sbin/nginx –s stop //关闭 nginx
8、测试
创建测试站点# mkdir -p /www/html/www.heytool.com
# mkdir -p /www/html/bbs.heytool.com
# echo "www.heytool.com" > /www/html/www.heytool.com/index.html
# echo "bbs.heytool.com" > /www/html/bbs.heytool.com/index.html
9、绑定hosts,测试
把两个域名指向 192.168.209.121192.168.209.121 www.heytool.com
192.168.209.121 bbs.heytool.com