nginx使用日志

nginx编译安装

安装configure编译环境:
yum install -y gcc gcc-c++ openssl-devel make
安装pcre库:
yum install -y pcre pcre-devel
pcre兼容正则表达式,安装pcre为了使nginx支持具备URI重写功能的Rewrite模块,如果不安装pcre库,则nginx无法使用rewrite模块功能,nginx的rewrite模块功能几乎是企业应用必须。
编译参数:
./configure --help
按需安装,如:

—with-http_stub_status_module
—with-http_ssl_module

查看已安装的模块(安装在/app下):
/app/nginx/sbin/nginx -V
如果编译安装后需要再添加模块,则重新运行./configure添加,然后make即可,注意不要make install,这样会覆盖原文件。

添加认证

需要httpd-tools工具:
yum install -y httpd-tools
如果是debain或者ubuntu系统,则
apt install -y apache2-utils
添加密码文件:
htpasswd -cb /app/nginx/htpasswd abc 123456
在对应模块(server)下添加:

1
2
auth_basic "Restricted Access";
auth_basic_user_file /app/nginx/htpasswd;

nginx模块功能

proxy代理模块

ngx_http_proxy_module proxy代理模块,用于把请求后抛给服务器节点或upstream服务器池,例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
worker_processes  1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools {
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=5;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://www_server_pools;
}
}

upstram负载均衡模块

ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
看官网nginx document upstream模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;

server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}

server {
location / {
proxy_pass http://backend; (uptream的标签名)
}
}

backend是模块名,随便起
server是http服务器
weight是权重,权重大的处理比例就多
backup是热备(高可用)

分享到