阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx编译安装与配置使用

165次阅读
没有评论

共计 6634 个字符,预计需要花费 17 分钟才能阅读完成。

第一部分 —-nginx 基本应用
源码编译安装 nginx
1、安装 pcre 软件包(使 nginx 支持 http rewrite 模块)
yum install -y pcre
yum install -y pcre-devel

2、安装 openssl-devel(使 nginx 支持 ssl)
yum install -y openssl-devel

3、创建用户 nginx
useradd nginx
passwd nginx

4、安装 nginx

[root@localhost ~]tar -vzxf nginx-1.11.3.tar.gz -C /usr/local
[root@localhost ~]cd nginx-1.11.3/
[root@localhost nginx-1.11.3]# ./configure \
> –group=nginx \
> –user=nginx \
> –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 \
> –http-client-body-temp-path=/tmp/nginx/client_body \
> –http-proxy-temp-path=/tmp/nginx/proxy \
> –http-fastcgi-temp-path=/tmp/nginx/fastcgi \
> –pid-path=/var/run/nginx.pid \
> –lock-path=/var/lock/nginx \
> –with-http_stub_status_module \
> –with-http_ssl_module \
> –with-http_gzip_static_module \
> –with-pcre
[root@localhost nginx-1.11.3]# make &&make install

5、修改配置文件 /etc/nginx/nginx.conf
# 全局参数设置
worker_processes  1;    #设置 nginx 启动进程的数量,一般设置成与逻辑 cpu 数量相同
error_log  logs/error.log;    #指定错误日志
worker_rlimit_nofile 102400;    #设置一个 nginx 进程能打开的最大文件数
pid        /var/run/nginx.pid;
     
events {
    worker_connections  1024;  #设置一个进程的最大并发连接数
}
 
#http 服务相关设置
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  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式
     
    sendfile        on; #是否调用 sendfile 函数输出文件,一般设置为 on,若 nginx 是用来进行磁盘 IO 负载应用时,可以设置为 off,降低系统负载
    gzip            on; #是否开启 gzip 压缩
    keepalive_timeout  65;      #设置长连接的超时时间
# 虚拟服务器的相关设置
    server {
        listen      80;        #设置监听的端口
        server_name  localhost;        #设置绑定的主机名、域名或 ip 地址
 
        charset koi8-r;    #设置编码字符
 
        location / {
            root  /var/www/nginx;      #设置服务器默认网站的根目录位置
            index  index.html index.htm;        #设置默认打开的文档
            }
 
        error_page  500 502 503 504  /50x.html;        #设置错误信息返回页面
        location = /50x.html {
            root  html;        #这里的绝对位置是 /var/www/nginx/html
        }
    }
 }

6、检测 nginx 配置文件是否正确
nginx -t

7、启动 nginx 服务

nginx

8、通过 nginx - s 控制 nginx 服务

nginx -s stop    #停止服务
nginx -s quit    #退出服务
nginx -s reopen    #重新打开日志文件
nginx -s reload    #重新加载配置文件

9、实现 nginx 开机自启
    1、vim /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/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
 
————————————————————————–

    2、添加权限

chmod +x /etc/init.d/nginx

    3、设置开机自启

chkconfig nginx on

10、nginx 日志文件详解
    nginx 日志文件分为 log_format 和 access_log 两部分

    log_format 定义记录的格式,其语法格式为
        log_format        样式名称        样式详情
    配置文件中默认有

log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                      ‘$status $body_bytes_sent “$http_referer” ‘
                      ‘”$http_user_agent” “$http_x_forwarded_for”‘;

变量说明

$remote_addr 和 $http_x_forwarded_for

客户端的 ip
$remote_user客户端的名称
$time_local访问时的本地时间
$request请求的 URL 和 http 协议
$status访问的状态码
$body_bytes_sent发送给客户端的主体内容大小
$http_referer记录客户端是从哪个页面链接访问过来的,若没有链接,则访问‘-’
$http_user_agent记录客户端使用的浏览器的相关信息

access_log 主要指定使用哪种格式记录和日志文件的位置,其语法格式为
access_log    日志文件路径        样式名称

    如:

access_log    /var/log/nginx/access.log    main;

下面是日志内容的截图示例

Nginx 编译安装与配置使用

第二部分 —–nginx 高级应用
    1、使用 alias 实现虚拟目录
location /lzs {
    alias /var/www/lzs;
    index index.html;        #访问 http://x.x.x.x/lzs 时实际上访问的是 /var/www/lzs/index/html

    2、通过 stub_status 模块监控 nginx 的工作状态

        1、通过 nginx - V 命令查看是否已安装 stnb_status 模块

Nginx 编译安装与配置使用

(可以发现已经安装了~~~)
       

        2、编辑 /etc/nginx/nginx.conf 配置文件

# 添加以下内容~~
location /nginx-status {
      stub_status on;
      access_log    /var/log/nginx/nginxstatus.log;    #设置日志文件的位置
      auth_basic    “nginx-status”;    #指定认证机制(与 location 后面的内容相同即可)
      auth_basic_user_file    /etc/nginx/htpasswd;        #指定认证的密码文件
      }

        3、创建认证口令文件并添加用户 lzs 和 zsgg,密码用 md5 加密

htpasswd -c -m /etc/nginx/htpasswd lzs
htpasswd -m /etc/nginx/htpasswd zsgg

        4、重启服务

        5、客户端访问 http://x.x.x.x/nginx-status 即可

 

    3、使用 limit_rate 限制客户端传输数据的速度

          1、编辑 /etc/nginx/nginx.conf

 location / {
    root    /var/www/nginx;
    index    index.html;
    limit_rate    2k;        #对每个连接的限速为 2k/s

            2、重启服务
注意要点:
    1、配置文件中的每个语句要以; 结尾

    2、使用 htpasswd 命令需要先安装 httpd

————————————– 分割线 ————————————–

Nginx 负载均衡配置实战  http://www.linuxidc.com/Linux/2014-12/110036.htm

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/134825.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计6634字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中