nginx tar.gz安装方法+简单静态文件配置

1.首先安装依赖包(依赖包有点多,我们采用yum的方式来安装)

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel  

 

2.将nginx-1.7.6.tar.gz上传到/usr/local/下

  

tar -zxvf nginx-1.7.6.tar.gz  

 

3.编译

  

cd nginx-1.7.6
./configure   
make
make install  

 

4.启动

## 检查配置文件是否正确
# /usr/local/nginx-1.6/sbin/nginx -t
# ./sbin/nginx -V # 可以看到编译选项
  
## 启动、关闭
# ./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定
# ./sbin/nginx -s stop
或 pkill nginx
  
## 重启,不会改变启动时指定的配置文件
# ./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`

 

5.将nginx添加到服务中

 直接复制nginx 文件到/etc/init.d/ 目录下即可

 

cp nginx /etc/inin.d/

 

然后可以通过service 的指令进行启动,由于是使用了nginx的脚本,所以指令的参数有些不同

1 启动: service nginx
2 停止:   service nginx -s stop
3 重启:   service nginx -s reload

 

自己编写启动脚本:

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/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" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

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 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    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  

 

6.加入开机启动

1.  vi  /etc/rc.local  

2. 在文件加入一行:/etc/init.d/nginx start

 

7.配置静态文件服务

  location ^~ /news_file {
          alias /data/attachment/DB40_file/news_file/;
                expires 30d;  #设置浏览器过期时间
                #目录或者文件不存在
                if (!-e $request_filename){
                   return 401;
                }
          #proxy_store on;  #开启缓存机制
          #proxy_store_access user:rw group:rw all:rw;  #缓存读写规则
          #proxy_temp_path b;       #存放静态文件的缓存目录
          #include proxy.conf;     #外联proxy理的详细配置如proxy_set_header, client_max_body_size ....
        }

8.配置访问的黑白名单

# 屏蔽单个ip访问
 
deny IP; 
# 允许单个ip访问
 
allow IP; 
# 屏蔽所有ip访问
 
deny all; 
# 允许所有ip访问
 
allow all; 
#屏蔽整个段即从123.0.0.1到123.255.255.254访问的命令
 
deny 123.0.0.0/8
#屏蔽IP段即从123.45.0.1到123.45.255.254访问的命令
 
deny 124.45.0.0/16
#屏蔽IP段即从123.45.6.1到123.45.6.254访问的命令
 
deny 123.45.6.0/24

一个实例:

    server {
        listen       8804;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        allow 121.15.210.97; --允许单个IP访问
        allow 172.18.5.0/24; --允许172.18.5.0 - 172.18.5.254 IP段访问
        deny all; --默认禁止所有IP访问

        location / {
            root   html;
            index  index.html index.htm;
        }

 

 

 

 

posted @ 2016-11-01 11:07  踏叶乘风  阅读(8375)  评论(0编辑  收藏  举报