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

Nginx入门教程

150次阅读
没有评论

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

Nginx 的编译

进入 /usr/local/src 目录下:
1)下载 nginx 的源码

wget http://nginx.org/download/nginx-1.10.2.tar.gz

2)解压源码包,进入解压后的包中(可以看到有 configure 文件):

./configure --prefix=/usr/local/nginx

3)在编译的过程中,可能会提示某些依赖库的缺失,安装即可:

yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel

(其中,devel 也是需要安装的,这个就相当于 c 语言中的头文件)
4)编译:

make && make install

5)编译成功之后,就进入到 /usr/local/nginx 目录下。

nginx 的启动

进入到 nginx 目录下发现四个目录,sbin 就是用来启动的:

./sbin/nginx 

这样默认启动,绑定的就是 80 号端口,如果你的 80 号端口已经被占用,就会出现如下提示的错误:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

关闭占用 80 端口的软件或者服务:

 netstat -tunlp | grep 80
 kill -9 进程号

杀掉和某个进程有关的所有进程:

pkill -9 nginx

查看和 nginx 有关的进程

ps aux | grep nginx

nginx 命令

1)信号控制与进程管理

TERM, INT Quick shutdown(不优雅,一般不采取)
QUIT Graceful shutdown(优雅的关闭进程, 即等请求结束后再关闭)
HUP Configuration reload ,Start the new worker processes with a new configuration Gracefully shutdown the old worker processes(改变配置文件, 平滑的重读配置文件)
USR1 Reopen the log files(重读日志, 在日志按月 / 日分割时有用)
USR2 Upgrade Executable on the fly(平滑的升级)
WINCH Gracefully shutdown the worker processes(优雅关闭旧的进程(配合 USR2 来进行升级))

2)具体语法:

  • Kill - 信号选项 nginx 的主进程号:
Kill -HUP 4873
  • Kill - 信号控制 cat /xxx/path/log/nginx.pid
Kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

(注意:/usr/local/nginx/logs/nginx.pid 这个文件就是 nginx 的进程号,所以,cat /usr/local/nginx/logs/nginx.pid直接输出的就是这个进程号,不用再去查进程号)

当然,不用这些命令也是可以的:

  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx-1.10.2/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

Nginx 配置段

// 全局区
worker_processes 1; // 有 1 个工作的子进程, 可以自行修改, 但太大无益, 因为要争夺 CPU, 一般设置为 CPU 数 * 核数

Event {// 一般是配置 nginx 连接的特性
// 如 1 个 word 能同时允许多少连接
 worker_connections  1024; // 这是指 一个子进程最大允许连 1024 个连接
}

http {// 这是配置 http 服务器的主要段
     Server1 {// 这是虚拟主机段
                  Location {// 定位, 把特殊的路径或文件再次定位 , 如 image 目录单独处理
            }             /// 如.php 单独处理
     }
     Server2 {}}

例子 1: 基于域名的虚拟主机

server {listen 80;  # 监听端口
        server_name a.com; # 监听域名

        location / {root /var/www/a.com;   # 根目录定位
                index index.html;
        }
    }

例子 2: 基于端口的虚拟主机配置

server {listen 8080;
        server_name 192.168.1.204;

        location / {
                root /var/www/html8080;
                index index.html;
        }
    }

日志管理

我们观察 nginx 的 server 段, 可以看到如下类似信息
#access_log logs/host.access.log main;
这说明 该 server, 它的访问日志的文件是 logs/host.access.log ,
使用的格式”main”格式.
除了 main 格式, 你可以自定义其他格式.

main 格式是什么?
log_format main‘remoteNginx 入门教程aNginx 入门教程 ddrNginx 入门教程 remote_user [timeNginx 入门教程 lNginx 入门教程ocal]Nginx 入门教程 request”’
#‘statusNginx 入门教程 body_bytes_sent“http_referer”’
    #‘”
http_user_agent” “$http_x_forwarded_for”’;

main 格式是我们定义好一种日志的格式, 并起个名字, 便于引用.
以上面的例子, main 类型的日志, 记录的 remote_addr…. http_x_forwarded_for 等选项.

1: 日志格式 是指记录哪些选项
默认的日志格式: main
log_format main‘remoteNginx 入门教程aNginx 入门教程 ddrNginx 入门教程 remote_user [timeNginx 入门教程 lNginx 入门教程ocal]Nginx 入门教程 request”’
status body_bytes_sent“httpreferer http_user_agent” “$http_x_forwarded_for”’;

如默认的 main 日志格式, 记录这么几项
远程 IP- 远程用户 / 用户时间 请求方法(如 GET/POST) 请求体 body 长度 referer 来源信息
http-user-agent 用户代理 / 蜘蛛 , 被转发的请求的原始 IP

http_x_forwarded_for: 在经过代理时, 代理把你的本来 IP 加在此头信息中, 传输你的原始 IP

2: 声明一个独特的 log_format 并命名

log_format  mylog '$remote_addr-"$request"''$status $body_bytes_sent "$http_referer" ''"$http_user_agent""$http_x_forwarded_for"';

在下面的 server/location, 我们就可以引用 mylog

在 server 段中, 这样来声明
Nginx 允许针对不同的 server 做不同的 Log ,(有的 web 服务器不支持, 如 lighttp)

access_log logs/access_8080.log mylog;
声明 log log 位置 log 格式;

实际应用: shell+ 定时任务 +nginx 信号管理, 完成日志按日期存储
分析思路:
凌晨 00:00:01, 把昨天的日志重命名, 放在相应的目录下
再 USR1 信息号控制 nginx 重新生成新的日志文件
具体脚本:

#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

定时任务
Crontab 编辑定时任务
01 00 * * * /xxx/path/b.sh 每天 0 时 1 分(建议在 02-04 点之间, 系统负载小)

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.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

Ubuntu 16.04 LTS 上安装 Nginx、MariaDB 和 HHVM 运行 WordPress http://www.linuxidc.com/Linux/2016-10/136435.htm

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

Linux(RHEL7.0)下安装 Nginx-1.10.2 http://www.linuxidc.com/Linux/2016-10/136484.htm

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

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

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

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