最近项目中用到了openresty框架作为后台服务。内部整合了nginx代理和lua脚本进行开发。前边在视频监控小项目中用到过nginx作为rtmp视频流服务器,但是仅写到了使用方法。今天主要针对nginx的基础使用进行记录,重点描述nginx的安装、部署、配置及各个模块结构,备日后查用。
安装
提到代理,通常的定义是对客户端进行代理,如此一来,在服务端便看不到客户端的真实ip。Nginx之所以被称为反向代理,是因为其代理的实服务端,也因此可以进行负载均衡的管理。
安装Nginx,首先需要安装依赖库。
sudo apt-get install openssl libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
然后下载Nginx源码:http://nginx.org/en/download.html。我下载的是1.16.0版本。其实Nginx也可以通过apt进行安装,这里是为了手动指定安装目录,才通过源码安装。下载完成后并解压,得到nginx-1.16.0文件夹。
pangdx@ubuntu:~/Downloads/nginx-1.16.0$ tree -L 1
.
├── auto
├── CHANGES
├── CHANGES.ru
├── conf
├── configure
├── contrib
├── html
├── LICENSE
├── Makefile
├── man
├── objs
├── README
└── src
进入到该目录,配置nginx安装路径
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf
随后通过makefile进行安装
make && make install
由于指定了安装路径,进入到安装路径下,启动nginx
cd /usr/loca/nginx
./nginx
如果安装成功,通过ps应该能看到后台进程。
pangdx@ubuntu:/usr/local/nginx$ ps aux | grep nginx
root 2414 0.0 0.0 32936 804 ? Ss 04:47 0:00 nginx: master process ./nginx
nobody 2415 0.0 0.0 37776 3488 ? S 04:47 0:00 nginx: worker process
pangdx 2417 0.0 0.0 21536 1076 pts/0 S+ 04:47 0:00 grep --color=auto nginx
常用指令
nginx -c path_of_nginx.conf #通过配置文件启动
nginx -s quit #停止
nginx -s reload #重启,重载配置文件
nginx -v #查看版本
nginx -t #查看配置文件目录
nginx -h #查看帮助信息
常用模块及配置
这部分把nginx自带的配置文件搬过来进行分析,同时会添加一个新的模块。添加完成后的配置文件不一定具有可用性,重在理解各个模块的作用。
#user nobody; #指定用户,默认为nobody
worker_processes 1; #指定worker线程个数,原则上与cpu个数一致
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #指定进程号存储文件
#deamon on|off #是否以后台形式开启
master_process off; #是否在同一主机上同时开启master和worker进程,不要修改
events {
use epoll; #指定使用epoll进行接入
worker_connections 1024; #最大连接个数,根据机器性能配置
}
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;
upstream backup {
server xxx.xxx.xxx.xxx weight=5 fail_timeout=30s max_fails=3;
#负载均衡备用机,指定权重、超时时间、失败次数
}
server {
listen 80; #监听的本地端口
server_name localhost; #域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #接入处理
proxy_pass http://backup; #对接负载均衡模块
root html; #客户端接入时,会将域名替换为root后的字段,在本机进行重定向,alias具有类似功能
index index.html index.htm;
#deny xxx.xxx.xxx.xxx #屏蔽ip
#allow xxx.xxx.xxx.1/255 #1~255对应的ip均允许接入
#denyall #全禁止
}
#error_page 404 /404.html;
# 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 {
# 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;
# }
#}
}
注释了nginx基本模块用法,附nginx帮助文档http://www.nginx.cn/doc/。
以上。