设为首页 收藏本站
查看: 1042|回复: 0

[经验分享] Nginx的安装及使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-9-23 10:06:29 | 显示全部楼层 |阅读模式
本节我们要讲的是关于nginx的内容,nginx是一个免费,开源,高性能的http服务器以及反向代理服务器,也是一个IMAP/POP3代理服务器。nginx因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。那么下面我们就来详细介绍下它吧。
  
  nginx特性
      基本功能:
          1、能够实现服务于静态文件,也就是静态资源的web服务器,能自动缓存打开的文件描述符;
          2、反向代理服务器,能够实现简单的负载均衡和冗余
          3、能够支持FastCGI协议
          4、有模块化话功能,但非DSO(动态装卸载)机制,支持多种过滤器gzip,SSI和完成图像大小调整等
          5、支持SSL功能
      扩展功能:
          1、能够基于名称和IP做虚拟主机
          2、支持keepalive
          3、支持平滑的配置更新或程序版本升级
          4、支持定制访问日志,支持使用日志缓存以提高性能
          5、支持url rewrite(地址重写)   
          6、支持路径别名
          7、支持基于IP及用户的认证
          8、支持速率限制,并发限制等
  
  nginx的编译安装
  编译安装之前必须准备好编译环境
  [iyunv@www ~]# yum install gcc openssl-devel pcre-devel zlib-devel
  创建运行所需要的用户
  [iyunv@www ~]# groupadd -r nginx
  [iyunv@www ~]# useradd -r -g nginx -s /bin/false -M nginx
  [iyunv@www ~]# tar xf nginx-1.6.1.tar.gz
   
    [iyunv@www ~]# cd nginx-1.6.1
  [iyunv@www nginx-1.6.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
  [iyunv@www nginx-1.6.1]# make && make install
  
  提供启动脚本:/etc/init.d/nginx
  
  #!/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="/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=([^ ]*).*//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
  
  创建需要的目录并启动服务
  [iyunv@www tmp]# mkdir -v /var/tmp/nginx/   
mkdir: created directory `/var/tmp/nginx/'   
[iyunv@www tmp]# service nginx start   
Starting nginx:                                            [  OK  ]
  
  配置文件解读
  nginx的配置文件分三段
  main配置段
   
  http配置段
   
  server配置段
  
  一些格式:
  listen配置的格式:(只能在server中进行设置)
  listen address:port[ default [ backlog=num | rcvbuf=size接收缓存大小 | sndbuf=size 发送缓存大小 | accept_filter=filter | deferred | bind | ssl 只能使用443端口]]
  listen 127.0.0.1:8000;
  listen 127.0.0.1;
  listen 8000;
  listen *:8000;
  listen localhost:8000;
  
  location配置的格式:(只能在server中进行设置)
  location [=|~|~*|^~|@] /url/ {...}
   
  例子
   
  
  下面是一个示例配置
  #/usr/local/ngnix/conf/ngnix.conf
  user nginx;                       运行服务的用户名
  worker_processes 4;         运行的进程数
  #error_log logs/error.log;        
  error_log /data/applogs/nginx/error.log notice;    错误日志
  #error_log logs/error.log info;
  pid /data/appdatas/nginx.pid;                            进程文件
  events {
      worker_connections 65535;                           最大连接数
  }
  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;                                                   
      #tcp_nopush on;
      #keepalive_timeout 0;
      keepalive_timeout 65;                              持久连接超时时间
    gzip on;                                                   是否压缩
    gzip_http_version 1.1;
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_proxied expired no-cache no-store private auth no_last_modified no_etag;
    gzip_types text/plain application/x-javascript text/css application/xml application/json;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
     include nginx_app.conf;                                  包含配置文件,定义server
  }
  
  
   nginx_app.conf
  server {   
    listen       80 ;                                    定义端口   
    server_name  www.mwj.com;              定义主机名
      #charset koi8-r;                                设定字符集
      #access_log  logs/host.access.log  main;     访问日志
      location / {   
        root   /usr/share/nginx/html;          设置服务器根目录   
        index  index.html index.htm;   
         auth_basic   "input password";                        用户认证功能   
       auth_basic_user_file  /etc/nginx/userfile ;         密码文件,自己定义
                  
    }
      error_page  404              /404.html;      404错误页面   
    location = /404.html {                          
        root   /usr/share/nginx/html;   
    }
      # redirect server error pages to the static page /50x.html   
    #   
    error_page   500 502 503 504  /50x.html;       50x错误页面   
    location = /50x.html {   
        root   /usr/share/nginx/html;   
    }
  }
  
  
  创建密码文件密码
  [iyunv@www ~]# htpasswd -c /etc/nginx/userfile admin                创建admin用户   
New password:     
Re-type new password:     
Adding password for user admin
  
  好了,下面就可以测试下了。
   
  输入账号密码,成功登陆
   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-25194-1-1.html 上篇帖子: nginx 基础知识解析 下篇帖子: nginx启动gzip压缩js文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表