Nginx常用功能

1、Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。

这里我给来2张图,对正向代理与反响代理做个诠释,具体细节,大家可以翻阅下资料。

Nginx在做反向代理时,×××能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,你就可以随心所欲的玩。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。

2、负载均衡

Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马行空,只有你想不到的没有他做不到的啦,你可以参照所有的负载均衡算法,给他一一找出来做下实现。

上3个图,理解这三种负载均衡算法的实现

Ip hash算法,对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题。 

3、web缓存

Nginx可以对不同的文件做不同的缓存处理,配置灵活,并且支持FastCGI_Cache,主要用于对FastCGI的动态程序进行缓存。配合着第三方的ngx_cache_purge,对制定的URL缓存内容可以的进行增删管理。

4、Nginx相关地址

源码:https://trac.nginx.org/nginx/browser

官网:http://www.nginx.org/


Nginx配置文件结构:

如果你下载好啦,你的安装文件,不妨打开conf文件夹的nginx.conf文件,Nginx服务器的基础配置,默认的配置也存放在此。

在nginx.conf的注释符号位#

nginx文件的结构,这个对刚入门的同学,可以多看两眼。

默认的config

 View Code

nginx文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}


1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

5、location块:配置请求的路由,以及各种页面的处理情况。

以下是对location快的详细解释:


语法规则: location [=|~|~*|^~] /uri/ { … }

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~*  开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

例子,有如下匹配规则: view plain copy

1. location  / {  
2.    #规则A  
3. }  
4. location  /login {  
5.    #规则B  
6. }  
7. location ^~ /static/ {  
8.    #规则C  
9. }  
10. location ~ \.(gif|jpg|png|js|css)$ {  
11.    #规则D  
12. }  
13. location ~* \.png$ {  
14.    #规则E  
15. }  
16. location !~ \.xhtml$ {  
17.    #规则F  
18. }  
19. location !~* \.xhtml$ {  
20.    #规则G  
21. }  
22. location / {  
23.    #规则H  
24. }

那么产生的效果如下:

访问根目录/, 比如http://localhost/ 将匹配规则A

访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H

访问 http://localhost/static/a.html 将匹配规则C

访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到 规则C

访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。

访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。

访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

所以实际使用中,通常至少有三个匹配规则定义,如下:

 copy

    1. #直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。  
    2. #这里是直接转发给后端应用服务器了,也可以是一个静态首页  
    3. # 第一个必选规则  
    4. location  / {  
    5.     proxy_pass http://tomcat:8080/index  
    6. }  
    7.    
    8. # 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项  
    9. # 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用  
    10. location ^~ /static/ {  
    11.     root /webroot/static/;  
    12. }  
    13. location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {  
    14.     root /webroot/res/;  
    15. }  
    16.    
    17. #第三个规则就是通用规则,用来转发动态请求到后端应用服务器  
    18. #非静态文件请求就默认是动态请求,自己根据实际把握  
    19. #毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了  
    20. location / {  
    21.     proxy_pass http://tomcat:8080/  
    22. }  
    23.


    以下为nginx配置文档nginx.conf的说明:

    #user administrator administrators;  #配置用户或者组,默认为nobody nobody。#worker_processes 2;  #允许生成的进程数,默认为1
    #pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
    error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
    events {
        accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
        multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
        #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
        worker_connections  1024;    #最大连接数,默认为512
    }
    http {
        include       mime.types;   #文件扩展名与文件类型映射表
        default_type  application/octet-stream; #默认文件类型,默认为text/plain
        #access_log off; #取消服务日志    
        log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
        access_log log/access.log myFormat;  #combined为日志格式的默认值
        sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
        sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
        keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
    
        upstream mysvr {   
          server 127.0.0.1:7878;
          server 192.168.10.121:3333 backup;  #热备
        }
        error_page 404 https://www.baidu.com; #错误页    
        server {
            keepalive_requests 120; #单连接请求上限次数。
            listen       4545;   #监听端口
            server_name  127.0.0.1;   #监听地址       
            location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
               #root path;  #根目录
               #index vv.txt;  #设置默认页
               proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
       
                      } 
        }
    }
       
    1、1.$remote_addr 与$http_x_forwarded_for: 用以记录客户端的ip地址; 
    2.$remote_user :用来记录客户端用户名称; 
     3.$time_local :
      4.$request : 用来记录请求的url与http协议;
      5.$status : 用来记录请求状态;成功是200,
    6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
    7.$http_referer :用来记录从那个页面链接访问过来的; 
      8.$http_user_agent :记录客户端浏览器的相关信息;
    2、惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。
    3、每个指令必须有分号结束。
    
    安装步骤:(实验环境redhat6.0-X64位系统)
    1、先下载nginx-1.4.1.tar.gz到服务器
    2、解压缩软件包
    3、创建系统用户和系统组nginx
    4、编译安装nginx
    
    [root@lamp ~]# lftp test@10.109.134.247      登录ftp服务器
    Password: 
    lftp test@10.109.134.247:/> get nginx-1.4.1.tar.gz  从ftp服务器下载安装包
    767107 bytes transferred                               
    lftp test@10.109.134.247:/> exit
    [root@lamp ~]#
    alldatabases.sql  Desktop    first.sql      Music     Pictures  stu.db 
    Documents  install.log     mysql-5.5.28        Public    Templates
    cmake-2.8.8    Downloads  install.log.syslog  nginx-1.4.1.tar.gz   nginx-1.4.1
    [root@lamp ~]# tar -xf nginx-1.4.1.tar.gz  解压缩安装包
    [root@lamp ~]#
    alldatabases.sql  Desktop    first.sql      Music     Pictures  stu.db 
    Documents  install.log     mysql-5.5.28        Public    Templates
    cmake-2.8.8    Downloads  install.log.syslog  nginx-1.4.1.tar.gz nginx-1.4.1
    [root@lamp ~]# du -sh nginx-1.4.1  查看目录占用的大小
    5.2Mnginx-1.4.1
    [root@lamp ~]# groupadd -r -g 108 nginx   创建系统组nginx并指定GID为108
    [root@lamp ~]# useradd -r -u 108 -g 108 nginx  创建系统用户nginx并指定GID和UID为108
    [root@lamp ~]# rpm -q pcre-devel  查询pcre-devel模块是否安装
    package pcre-devel is not installed
    [root@lamp ~]# yum -y install pcre-devel  安装pcre-devel模块,此模块为nginx所依赖的模块
    Loaded plugins: refresh-packagekit, rhnplugin
    This system is not registered with RHN.
    RHN support will be disabled.
    Server                                                                       | 3.7 kB     00:00 ... 
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ................
    Running Transaction
    Warning: RPMDB altered outside of yum.
      Installing     : pcre-devel-7.8-3.1.el6.x86_64            1/1 
    Installed:
      pcre-devel.x86_64 0:7.8-3.1.el6                             
    Complete!   安装完成
    [root@lamp ~]# cd nginx-1.4.1
    [root@lamp nginx-1.4.1]# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre=/usr --with-file-aio --with-http_p_w_picpath_filter_module   编译安装指定安装路径--prefix,指定执行命令路径--sbin-path,指定配置文档路径--conf-path,指定错误日志路径--error-log-path,指定http访问日志路径--http-log-path,指定pid路径--pid-path等操作。
    
    checking for GD library in /usr/local/ ... not found
    checking for GD library in /usr/pkg/ ... not found
    checking for GD library in /opt/local/ ... not found
    
    ./configure: error: the HTTP p_w_picpath filter module requires the GD library.
    You can either do not enable the module or install the libraries.
    提示GD库没有安装,无法支持p_w_picpath filter module模块,解决方法如下,安装gd-2.0.35和gd-devel-2.0.35即可。
    
    lftp test@10.109.134.247:/> mget gd-2.0.35-11.el6.x86_64.rpm  gd-devel-2.0.35-11.el6.x86_64.rpm   从ftp服务器上获取gd和gd-devel的rpm包进行安装。
    [root@lamp ~]# rpm -ivh gd-2.0.35-11.el6.x86_64.rpm 
    warning: gd-2.0.35-11.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
    Preparing...                ########################################### [100%]
       1:gd                     ########################################### [100%]
    [root@lamp ~]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm
    warning: gd-devel-2.0.35-11.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
    Preparing...                ########################################### [100%]
       1:gd-devel               ########################################### [100%]
    [root@lamp ~]# cd nginx-1.4.1
    [root@lamp nginx-1.4.1]# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre=/usr --with-file-aio --with-http_p_w_picpath_filter_module
    checking for OS
     + Linux 2.6.32-71.el6.x86_64 x86_64
    checking for C compiler ... found
     + using GNU C compiler
     + gcc version: 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) 
    ............................................
      nginx configuration prefix: "/etc/nginx"
      nginx configuration file: "/etc/nginx/nginx.conf"
      nginx pid file: "/var/run/nginx/nginx.pid"
      nginx error log file: "/var/log/nginx/error.log"
      nginx http access log file: "/var/log/nginx/access.log"
      nginx http client request body temporary files: "/var/tmp/nginx/client/"
      nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
      nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"
      nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"
      nginx http scgi temporary files: "/var/tmp/nginx/scgi"
    #再次执行编译时,没有报错,继续执行编译安装
    [root@lamp nginx-1.4.1]# make    安装时报错,需取消编译安装时--with-pcre的指定路径
    make -f objs/Makefile
    make[1]: Entering directory `/root/nginx-1.4.1'
    cd /usr \
    && if [ -f Makefile ]; then make distclean; fi \
    && CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
    ./configure --disable-shared 
    /bin/sh: line 2: ./configure: No such file or directory
    make[1]: *** [/usr/Makefile] Error 127
    make[1]: Leaving directory `/root/nginx-1.4.1'
    make: *** [build] Error 2
    [root@lamp nginx-1.4.1]# make clean   清空编译缓存
    [root@lamp nginx-1.4.1]# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --with-file-aio --with-http_p_w_picpath_filter_module   **此处不指定--with-pcre路径,再次编译  
     .........................
      nginx path prefix: "/usr"
      nginx binary file: "/usr/sbin/nginx"
      nginx configuration prefix: "/etc/nginx"
      nginx configuration file: "/etc/nginx/nginx.conf"
      nginx pid file: "/var/run/nginx/nginx.pid"
      nginx error log file: "/var/log/nginx/error.log"
      nginx http access log file: "/var/log/nginx/access.log"
      nginx http client request body temporary files: "/var/tmp/nginx/client/"
      nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
      nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"
      nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"
    编译完成
    [root@lamp nginx-1.4.1]# make   #执行make
    objs/src/http/modules/ngx_http_flv_module.o \
    objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \
    objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
    objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
    objs/src/http/modules/ngx_http_stub_status_module.o \
    objs/ngx_modules.o \
    -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lz -lgd
    make[1]: Leaving directory `/root/nginx-1.4.1'
    make -f objs/Makefile manpage
    make[1]: Entering directory `/root/nginx-1.4.1'
    sed -e "s|%%PREFIX%%|/usr|" \
    -e "s|%%PID_PATH%%|/var/run/nginx/nginx.pid|" \
    -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
    -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
    < man/nginx.8 > objs/nginx.8
    make[1]: Leaving directory `/root/nginx-1.4.1'    编译完成
    [root@lamp nginx-1.4.1]# make install
    make -f objs/Makefile install
    make[1]: Entering directory `/root/nginx-1.4.1'
    test -d '/usr' || mkdir -p '/usr'
    test -d '/usr/sbin' || mkdir -p '/usr/sbin'
    test ! -f '/usr/sbin/nginx' || mv '/usr/sbin/nginx' '/usr/sbin/nginx.old'
    cp objs/nginx '/usr/sbin/nginx'
    test -d '/etc/nginx' || mkdir -p '/etc/nginx'
    cp conf/koi-win '/etc/nginx'
    cp conf/koi-utf '/etc/nginx'
    cp conf/win-utf '/etc/nginx'
    test -f '/etc/nginx/mime.types' || cp conf/mime.types '/etc/nginx'
    cp conf/mime.types '/etc/nginx/mime.types.default'
    test -f '/etc/nginx/fastcgi_params' || cp conf/fastcgi_params '/etc/nginx'
    cp conf/fastcgi_params '/etc/nginx/fastcgi_params.default'
    test -f '/etc/nginx/fastcgi.conf' || cp conf/fastcgi.conf '/etc/nginx'
    cp conf/fastcgi.conf '/etc/nginx/fastcgi.conf.default'
    test -f '/etc/nginx/uwsgi_params' || cp conf/uwsgi_params '/etc/nginx'
    cp conf/uwsgi_params '/etc/nginx/uwsgi_params.default'
    test -f '/etc/nginx/scgi_params' || cp conf/scgi_params '/etc/nginx'
    cp conf/scgi_params '/etc/nginx/scgi_params.default'
    test -f '/etc/nginx/nginx.conf' || cp conf/nginx.conf '/etc/nginx/nginx.conf'
    cp conf/nginx.conf '/etc/nginx/nginx.conf.default'
    test -d '/var/run/nginx' || mkdir -p '/var/run/nginx'
    test -d '/var/log/nginx' || mkdir -p '/var/log/nginx'
    test -d '/usr/html' || cp -R html '/usr'
    test -d '/var/log/nginx' || mkdir -p '/var/log/nginx'
    make[1]: Leaving directory `/root/nginx-1.4.1'   编译安装完成

    为nginx提供系统脚本,脚本内容如下:

    新建文件/etc/rc.d/init.d/nginx内容如下:

    ******************************************************************
    [root@lamp nginx-1.4.1]# vim /etc/rc.d/init.d/nginx
    
    #!/bin/bash
    #
    # 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/sbin/nginx"
     prog=$(basename $nginx)
     
     NGINX_CONF_FILE="/etc/nginx/nginx.conf"
     
     [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
     
     lockfile=/var/lock/subsys/nginx
     
     make_dirs() {
       # make required directories
       user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
     for opt in $options; do
        if [ `echo $opt | grep '.*-temp-path'` ]; then
          value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
            # echo "creating" $value
            mkdir -p $value && chown -R $user $value
           fi
         fi
     done
    }
    
    start() {
       [ -x $nginx ] || exit 5
       [ -f $NGINX_CONF_FILE ] ||exit 6
       make_dirs
       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 
       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
    *****************************************************************************

    而后为此脚本赋予执行权限:

    chmod +x /etc/rc.d/init.d/nginx
    [root@lamp nginx-1.4.1]# chmod +x /etc/rc.d/init.d/nginx 
    [root@lamp nginx-1.4.1]# chkconfig --add nginx  #增加到开机运行列表
    [root@lamp nginx-1.4.1]# chkconfig nginx on  启用开机运行nginx
    [root@lamp nginx-1.4.1]# chkconfig --list nginx
    nginx          0:off1:off2:on3:on4:on5:on6:of
    [root@lamp nginx-1.4.1]# service nginx start
    Starting nginx:                       [  OK  ]

    然后通过浏览器访问该IP地址即可:

    linux中的nginx一般放在那个目录 linux的nginx命令_Nginx

    实例:测试nginx基于用户访问控制

    首先需安装httpd服务,但是千万不要开启httpd服务,只是提供htpasswd用户认证文件生成的功能,所以需关闭httpd服务,且不能让其自动启动,安装httpd直接用yum -y install httpd 安装后通过htpasswd生成用户密码访问文件。

    [root@lamp usr]# chkconfig --list httpd  #查询httpd是否开机启动,确保开机不会自动启动,且确保该httpd服务处于关闭状态。
    httpd          0:off1:off2:off3:off4:off5:off6:off
    [root@lamp usr]# service httpd status #查询httpd服务状态
    httpd is stopped
    [root@lamp usr]##第一次生成用户时需-c参数
    New password: 
    Re-type new password: 
    Adding password for user tom
    [root@lamp usr]# htpasswd  -m /etc/nginx/.user_file jerry #第二次及以后生成用户时不能使用-c参数,请牢记。
    New password: 
    Re-type new password: 
    Adding password for user jerry

    生成用户访问控制文件后,编辑nginx.conf配置文档进行相关修改:

    [root@lamp usr]# #编辑配置文档在需要通过访问控制的主机的location中添加控制功能的选项,如下红色框内。

    linux中的nginx一般放在那个目录 linux的nginx命令_运维_02

    [root@lamp usr]# service nginx restart
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    Stopping nginx:                                     [  OK  ]
    Starting nginx:                                     [  OK  ]

    访问提示需要用户名和密码

    linux中的nginx一般放在那个目录 linux的nginx命令_Nginx_03








    nginx-1.4.1.tar.gz下载地址:http://nginx.org/download/

    gd-devel-2.0.35下载地址:

    gd-2.0.35下载地址:http://rpmfind.net/linux/rpm2html/search.php?query=gd&submit=Search+...


    转载于:https://blog.51cto.com/woyaoxuelinux/1953038