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

凉白开 Nginx441,3624字数 4002阅读13分20秒阅读模式

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

环境文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

操作系统:CentOS、RedHat文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

IP地址:192.168.1.202文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

下载软件包

# mkdir /usr/local/src/tarbag文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# mkdir /usr/local/src/software文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# cd /usr/local/src/tarbag/文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

Nginx文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# wget http://www.nginx.org/download/nginx-1.0.6.tar.gz文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

Nginx cache purge模块(可选)文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

编译安装

# cd /usr/local/src/tarbag/文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# tar -xzvf nginx-1.0.6.tar.gz -C /usr/local/src/software文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# tar -xzvf ngx_cache_purge-1.3.tar.gz -C /usr/local/src/software文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# cd /usr/local/src/software/文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# ./configure \文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

--prefix=/usr/local/nginx-1.0.6 \  # 安装路径文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

--with-http_stub_status_module \ # 启用nginx状态模块文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

--with-http_ssl_module \ # 启用SSL模块文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

--with-http_realip_module \ # 启用realip模块(将用户IP转发给后端服务器)文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

--add-module=../ngx_cache_purge-1.3 # 添加缓存清除扩展模块文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/

# 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,如下图:

 

nginx

nginx


打开bbs.heytool.com,如下图:

nginx

nginx

 

完毕!!!!

更多配置参考:http://www.docin.com/p-222277825.html

参考视频教程:http://www.kuaipan.cn/index.php?ac=file&oid=29331332391763978

文章推荐:

Haproxy+keepalived实现sphinx高可用负载均衡

使用haproxy来实现sphinx负载均衡与健康监测

持久化存储系统ttserver的安装配置

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 22/06/2013 13:11:56
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-modules-ngx_cache_purge/
  • nginx
  • nginx_cache
  • nginx安装
  • nginx安装配置
  • nginx配置
  • ngx_cache
  • purege
  • purge安装
  • web容器
评论  4  访客  3
    • 风
      9

      内核参数优化 报出很多错误:
      error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
      error: "net.bridge.bridge-nf-call-iptables" is an unknown key
      error: "net.bridge.bridge-nf-call-arptables" is an unknown key
      上面三个还是未改动之前的配置文件里面就有的
      error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_established" is an unknown key
      error: "net.ipv4.ip_conntrack_max" is an unknown key
      error: "net.ipv4.netfilter.ip_conntrack_max" is an unknown key

      修改nginx配置文件 这一步,有警告信息 nginx: [warn] server name "/usr/local/nginx/html/bbs" has suspicious symbols in /usr/local/nginx/conf/nginx.conf:86

      是有哪里出的错误
      系统是centos6,3 x64, nginx 1.6.2

        • 默北
          默北 6

          @ 内核参数这些,要么是相关模块没有加载,要么是名称改变了。 这个查看centos6版本的文档。

          nginx 的 看看86行,是不是有特殊符号

            • 风
              9

              @ 默北 谢谢,原来是85行行末少了个分号。太大意了

        评论已关闭!