经过一段时间的使用,发现nginx在并发与负载能力方面确实优于apache,现在已经将大部分站点从apache转到了nginx了。以下是nginx的一些简单的安装配置。 环境 操作系统:CentOS、RedHat IP地址:192.168.1.202 下载软件包# 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 \ # 安装路径 –with-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.heytool.com /www/html/www.heytool.com 2 bbs.heytool.com /www/html/bbs.heytool.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.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; } } } 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.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 启动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.heytool.com 192.168.1.202 bbs.heytool.com 打开www.heytool.com,如下图:
打开bbs.heytool.com,如下图:
完毕!!!! 更多配置参考:http://www.docin.com/p-222277825.html 参考视频教程:http://www.kuaipan.cn/index.php?ac=file&oid=29331332391763978 文章推荐: Haproxy+keepalived实现sphinx高可用负载均衡 使用haproxy来实现sphinx负载均衡与健康监测 持久化存储系统ttserver的安装配置 (责任编辑:IT) |