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

[经验分享] Nginx基本使用方法及各模块基本功能

[复制链接]

尚未签到

发表于 2018-11-11 09:28:49 | 显示全部楼层 |阅读模式
1、安装依赖包(CentOS 6.7)  
[root@mail soft]# yum -y groupinstall "Development tools,Server platform development,Desktop platform development"
  
[root@mail soft]# yum -y install pcre-devel openssl-devel
  

  
2、编译安装Nginx
  
[root@mail soft]# tar xf nginx-1.8.0.tar.gz
  
[root@mail soft]# cd nginx-1.8.0
  
[root@mail nginx-1.8.0]# groupadd -r nginx
  
[root@mail nginx-1.8.0]# useradd -g nginx -s /sbin/nologin -M nginx
  
[root@mail nginx-1.8.0]# ./configure \
  
   --prefix=/usr/local/nginx \
  
   --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
  
[root@mail nginx-1.8.0]# make && make install
  

  
##注意:编译后有些文件夹不会自动创建
  

  
[root@mail nginx-1.8.0]# mkdir -pv /var/tmp/nginx/{client,proxy,fcgi,uwsgi,scgi}
  
mkdir: created directory `/var/tmp/nginx'
  
mkdir: created directory `/var/tmp/nginx/client'
  
mkdir: created directory `/var/tmp/nginx/proxy'
  
mkdir: created directory `/var/tmp/nginx/fcgi'
  
mkdir: created directory `/var/tmp/nginx/uwsgi'
  
mkdir: created directory `/var/tmp/nginx/scgi'
  

  
这里的二进制文件是直接指定在PATH环境变量里面的,所有可以直接使用,不用导出:
  

  
配置vim,使其编辑nginx配置文件时语法着色,默认没有
  
[root@mail nginx-1.8.0]# cd
  
[root@mail ~]# mkdir .vim
  
[root@mail ~]# cp -ra /u01/soft/nginx-1.8.0/contrib/vim/* .vim
  
[root@mail ~]# ls .vim
  
ftdetect  indent  syntax
  

  
编写启动脚本:
  
[root@mail ~]# vim /etc/rc.d/init.d/nginx
  
#!/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:      /etc/nginx/nginx.conf
  
# 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"
  
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
  

  
[root@mail ~]# chmod +x /etc/rc.d/init.d/nginx   #添加可执行权限
  
[root@mail ~]# chkconfig --add nginx   #添加到服务列表
  
[root@mail ~]# chkconfig nginx on      #设置开机自启动
  

  
[root@mail ~]# service nginx start   #启动nginx
  
Starting nginx:                                            [  OK  ]
  
[root@mail ~]# ss -tnl     #查看是否监听80端口
  
[root@mail ~]# ss -tnl
  
State       Recv-Q Send-Q                Local Address:Port                  Peer Address:Port
  
LISTEN      0      128                               *:80                               *:*
  
LISTEN      0      128                              :::22                              :::*
  
LISTEN      0      128                               *:22                               *:*
  
##查看页面
  
[root@mail ~]# curl http://localhost
  

  

  

  
Welcome to nginx!
  

  
    body {
  
        width: 35em;
  
        margin: 0 auto;
  
        font-family: Tahoma, Verdana, Arial, sans-serif;
  
    }
  

  

  

  
Welcome to nginx!
  
If you see this page, the nginx web server is successfully installed and
  
working. Further configuration is required.
  

  
For online documentation and support please refer to
  
nginx.org.
  
Commercial support is available at
  
nginx.com.
  

  
Thank you for using nginx.
  

  

  
#######################编译安装已经完成。
  

  
配置文件介绍
  
  主要有两部分:分别是
  
    main:主体部分
  
    http{}:虚拟主机配置部分
  

  
   配置指令主要以分号结尾;配置语法:directive value1 [value2 ....]
  
   支持使用的变量
  
       模块内置的变量
  
       自定义变量:set var_name value
  

  
   主配置段的指令类别:
  
  用于调试和定位问题:
  
   (1)daemon [on|off]:  是否以守护进程的方式启动nginx;
  
   (2)master_press [on|off]:  是否以master/worker模型来运行nginx;
  
   (3)error_log /path/to/error_loglevel:  指明错误日志文件级别,处于调试目的,可以使用debug级别,但次级别只有在编译nginx时使用了--with-debug选项才有效 ;
  

  
  正常运行必备的配置:
  
   (1)user USERNAME [GROUPNAME]:指定运行worker的用户和用户组;例如 user nginx nginx
  
   (2)pid /path/to/nginx.pid : 指定pid文件
  
   (3)worker_rlimit_nofile # : 指定一个worker进程能够打开的最大文件句柄数
  
   (4)worker_rlimit_sigpending # : 指定每个用户能够发往worker信号的数量
  

  
  优化性能相关的配置:
  
   (1)worker_processes # :worker进程的个数,通常是cpu核心数减1
  
   (2)worker_cpu_affinity cpuumask :绑定worker进程至指定的CPU上
  
   (3)timer-resolution t :时间解析度,在x86服务器上可以不用配置
  
   (4)worker_priority NICE :调整nice值(-20,19);nice值越大,越优先分配cpu
  

  
  事件相关的配置;
  
   (1)accept_mutex [on|off] :内部调动用户请求至各worker时的负载均衡锁;启用时表示能够让多个worker轮流的、序列化的响应请求
  
   (2)lock_file /path/to/lock_file :指定锁文件
  
   (3)accept_mutex_delay #ms: 取得负载均衡锁的时间
  
   (4)use [epoll|poll|select|rgsig]:定义使用的事件模型,建议让nginx自动选择
  
   (5)worker_connections #:每个worker进程所能够响应的最大并发请求数



运维网声明 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.iyunv.com/thread-633553-1-1.html 上篇帖子: 5分钟搭建 nginx +php -----(LNMP)新手专用 下篇帖子: Keepalived+Nginx实现双主高可用负载均衡
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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