一、nginx的概述,参考
二、nginx的安装,参考:
windows的nginx安装包下载地址:nginx: download
1.安装编译工具及库(gcc、g++、openssl、zlib库、PCRE库(支持 Rewrite 功能))
据说是由于Nginx官方提供的是C源码,要自己进行编译,所以要安装编译所依赖的环境和库才可正常编译(我安装nginx时没有安装过这些工具,所以不太清楚必要不,从网上找到两种安装的方法记下来,以备后续重新安装需要)
方法一:合并安装
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
方法二:分别安装
安装gcc
yum -y install gcc automake autoconf libtool make
安装g++
yum install gcc gcc-c++
安装openssl
yum -y install openssl openssl-devel
安装zlib库
cd /usr/local/ #先进入该目录
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
安装PCRE库
cd /usr/local/ #进入该目录下安装
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz #网上下载安装包
tar zxvf pcre-8.35.tar.gz #解压
cd pcre-8.35
./configure
make && make install
pcre-config --version #用来查看PCRE版本
2.安装nginx
一般默认在/usr/local/目录下安装nginx,也可以指定目录安装
#进入安装目录
cd /usr/local/
#网上下载压缩包
wget http://nginx.org/download/nginx-1.17.8.tar.gz
#解压
tar zxvf nginx-1.17.8.tar.gz
#重命名文件
mv nginx-1.17.8 nginx
cd nginx
#执行配置
./configure
#编译安装
make && make install
#查看nginx版本
nginx -v
注:如果要指定配置文件存放的目录或者执行文件存放的目录等,则可以在执行配置文件的时候加上路径:
#将nginx的配置文件和执行文件存放在/home/work/software/nginx目录下
./configure --conf-path=/home/work/software/nginx --sbin-path=/home/work/software/nginx
关于./configure的其他参数用法可以参考官方文档:Building nginx from Sources
安装完后,nginx目录下会生成以下几个文件:
conf:nginx运行环境的配置文件,其中nginx.conf是主配置文件
sbin:目录下只有一个 nginx
可执行文件,是 Nginx
服务器的主程序
logs:存放nginx的日志文件
accsee.log是访问日志,主要记录了客户端的每一条请求信息
error.log是错误日志,有不同的日志输出级别,可以帮助我们尽快定位到错误原因
nginx.pid用来存放nginx的master进程的进程号
html:nginx的页面文件,默认有两个html
3.nginx的常用命令
①测试nginx的配置文件是否有语法错误
#执行该命令前先进入到nginx的sbin目录下,一般默认路径是/usr/local/nginx/sbin
/nginx -t
②启动nginx
./nginx
注意:不能重复启动,如果已经启动了,在执行./nginx命令会报端口被占用的错误
③重新加载nginx的配置文件
#前提是nginx已经启动了,配置文件进行了修改,且修改后检查无错误
./nginx -s reload
④重新启动nginx
#前提nginx已经启动
./nginx -s reopen
注1:reload跟reopen的区别
reload用于加载修改后的配置文件的重启
reopen用于重新打开日志文件
具体区别详解参考:nginx reload和reopen - 那些年的代码 - 博客园
注2:当我们关掉nginx或者杀死nginx进程号的时候,使用reload或者reopen会重启失败,报以下错误:
这是因为nginx停止时,用来存放nginx的master进程号的nginx.pid也会被删除掉,而 reload和 reopen命令需要通过nginx.pid获取进程号,nginx.pid被删除后,reload和reopen找不到就会报错
此时就可以使用./nginx -c 指定配置文件启动nginx(参考第⑧点)
⑤停止nginx
#前提nginx已经启动
./nginx -s stop
⑥退出nginx
#前提nginx已经启动
./nginx -s quit
注:stop跟quit的区别
stop是强制停止nginx服务器,如果当前有未处理的请求数据,直接丢弃
quit是优雅的停止nginx服务器,如果有未处理的请求数据,等待处理完成之后再停止
具体区别详解参考官方文档:Beginner’s Guide
⑦-p 指明nginx启动的配置目录
./nginx -p /home/work/software/nginx
⑧-c 指定配置文件启动nginx
./nginx -c /home/work/software/nginx/conf/nginx.conf
注:c参数填写配置文件的实际路径即可,不使用-c参数时默认使用nginx安装目录下conf/nginx.conf
⑨通过进程号重启、停止nginx
查看进程号
ps -ef|grep nginx
master是主进程号
停止进程号
kill -9 <master进程号> #强制停止
kill -QUIT <master进程号> #从容停止
kill -TERM <master进程号> #快速停止
通过进程号重启nginx
kill -HUP <master进程号>
4.nginx.conf配置文件参数解释
#Nginx worker进程运行的用户及用户组
#语法:user username[groupname] 默认:user nobody nobody
#user用于设置master进程启动后,fork出的worker进程运行在那个用户和用户组下。当按照"user username;"设置时,用户组名与用户名相同。
#若用户在configure命令执行时,使用了参数--user=usergroup 和 --group=groupname,此时nginx.conf将使用参数中指定的用户和用户组。
#user nobody;
worker_processes 1; #nginx的工作进程,如果没有阻塞式调用,建议进程个数和CPU内核数相同,如果有阻塞式调用,建议比CPU内核数稍大一点# 错误日志的存放位置和级别(debug | info | notice | warn | error | crit,从左至右级别依次增大)
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#进程文件存放位置
#pid logs/nginx.pid;
events {
worker_connections 1024; #单个进程最大连接数(最大连接数=连接数*进程数);理论上每台nginx服务器的最大连接数为:worker_processes*worker_connections
}
#设置http服务器
http {
include mime.types; #设定mime类型,类型由mime.types文件定义,是文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain#设置日志格式,其中main为格式的名称,可以设置多个不同的格式
#$remote_addr:与$http_x_forwarded_for 用以记录客户端的ip地址
#$remote_user:用来记录客户端用户名称
#$time_local:用来记录访问时间与时区
#$request:用来记录请求的url与http协议
#$status:用来记录请求状态;成功是200
#$body_bytes_sent:记录发送给客户端文件主体内容大小
#$http_referer:用来记录从那个页面链接访问过来的
#$http_user_agent:记录客户浏览器的相关信息
#通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';#combined为日志格式的默认值,用了log_format指令设置了日志格式为main之后,需要用access_log指令指定日志文件的存放路径
#access_log logs/access.log main;#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
sendfile on;
#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#tcp_nopush on;#长连接超时时间,单位是秒;默认为75s,可以在http,server,location块
#keepalive_timeout 0;
keepalive_timeout 65;#开启gzip压缩输出,对传输的网页进行压缩
#gzip on;server {
#监听端口, 监听80端口时, 必须用root账户启动nginx
listen 8010;
#域名可以有多个,用空格隔开
server_name localhost;#charset koi8-r;
#access_log logs/host.access.log main;
# server作为静态web服务器,服务重定向到本地的静态资源目录
# "/" 可以替换为其他表达式
# 以下配置,则访问http://localhost的时候,请求重定向到nginx安装目录下html中的index.html
location / {
# root指定重定向后uri的资源查找路径,这里html为相对路径,相对于nginx的安装目录
# 默认在nginx的安装目录中会有一个html目录,里面有index.html, 50x.html文件
root html;
#index指定首页index文件的名称,可以配置多个,参数以空格分开,按配置顺序查找
index index.html index.htm;
}#error_page 404 /404.html; #设置404访问页面,可使用绝对或相对路径
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启location = /50x.html {
root html;
}# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
# 可以配置多个server块,但是两个server块的配置中listen和server配置不能一样,否则报错
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;# location / {
# root html;
# index index.html index.htm;
# }
#}
server{
listen 8011;
server_name localhost;
sendfile on;
location / {
proxy_pass http://send;
}
location /data/detail/related {
proxy_pass http://www.baidu.com/web/santu_dl;
}
location /data/list {
proxy_pass http://www.baidu.com/web/video_response;
}
}
upstream send {
server 10.174.35.21:8032;
}server{
listen 8012;
server_name localhost;
sendfile on;
location / {
proxy_pass http://ads;
}
location /data/detail/ads {
proxy_pass http://12334444444.com;
}
}
upstream ads {
server 10.174.34.31:8057;
}
}
参考文档:
Nginx 配置详解 | 菜鸟教程