码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 学习笔记一

时间:2015-08-08 06:53:30      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:nginx 学习笔记

Nginx 是一个高性能的 HTTP 和 反向代理 服务器

安装nginx

Nginx依赖

    安装 pcre,支持 rewrite

yum install pcre*

    安装 openssl,需要 ssl 的支持

 yum install openssl*


安装

    # ./configure --prefix=/usr/local/nginx \    
    --with-http_ssl_module   \    
    --with-http_stub_status_module 
    --with-pcre
    #–with-http_stub_status_module:支持 nginx 状态查询
    #–with-http_ssl_module:支持 https
    #–with-pcre:为了支持 rewrite 重写功能,必须制定 pcre
    
    make && make install

启动 关闭 nginx

    启动  /usr/local/nginx/sbin/nginx

    关闭   /usr/local/nginx/sbin/nginx -s stop

    或者

    添加启动脚本

    

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemin 
# 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
# proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config: /usr/local/nginx/conf/nginx.conf 
# pidfile: /usr/local/nginx/logs/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

lockfile=/var/lock/subsys/nginx 

start() { 
[ -x $nginx ] || exit 5 
[ -f $NGINX_CONF_FILE ] || exit 6 
echo -n $"Starting $prog: " 
daemon $nginx -c $NGINX_CONF_FILE 
retval=$? 
echo 
[ $retval -eq 0 ] && touch $lockfile 
return $retval 
} 

stop() { 
echo -n $"Stopping $prog: " 
killproc $prog -QUIT 
retval=$? 
echo 
[ $retval -eq 0 ] && rm -f $lockfile 
return $retval 
} 

restart() { 
configtest || return $? 
stop 
start 
} 

reload() { 
configtest || return $? 
echo -n $"Reloading $prog: " 
killproc $nginx -HUP 
RETVAL=$? 
echo 
} 

force_reload() { 
restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
status $prog 
} 

rh_status_q() { 
rh_status >/dev/null 2>&1 
} 

case "$1" in 
start) 
rh_status_q && exit 0 
$1 

stop) 
rh_status_q || exit 0 
$1 

restart|configtest) 
$1 

reload) 
rh_status_q || exit 7 
$1 

force-reload) 
force_reload 

status) 
rh_status 

condrestart|try-restart) 
rh_status_q || exit 0 

*) 
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
exit 2 
esac


nginx添加清缓存模块安装

tar xf ngx_cache_purge-2.3.tar.gz -C /usr/local/ngx_cache_purge
进入nginx源文件 重新配置
# ./configure --prefix=/usr/local/nginx \    
    --with-http_ssl_module   \    
    --with-http_stub_status_module 
    --with-pcre
    --add-module=/usr/local/ngx_cache_purge
make 不需要make install
复制obj目录下面的nginx二进制文件到/usr/local/nginx/sbin下
cp obj/nginx /usr/local/nginx/sbin

查看加载的模块 nginx -V

内核参数优化

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

简单站点配置

server {
        listen       80;
        # 在本机所有 ip 上监听 80
        server_name  www.jwh5566.com;#域名
        location / {
            index  index.html index.htm;# 索引文件
        }
        error_page   500 502 503 504  /50x.html;
        #定义错误页面,如果是 500 错误,则把站点根目录下的 50x.html 返回给用户
        location = /50x.html {
            root   /www/html/www.jwh5566.com;站点根目录
        }

测试

    mkdir –p /www/html/www.jwh5566.com

    echo “www.jwh5566.com” > /www/html/www.jwh5566.com/index.html 

    打开浏览器访问http://www.jwh5566.com即可!








本文出自 “Linux is belong to you” 博客,请务必保留此出处http://jwh5566.blog.51cto.com/7394620/1682791

Nginx 学习笔记一

标签:nginx 学习笔记

原文地址:http://jwh5566.blog.51cto.com/7394620/1682791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!