一、数据采集器
- log_format:日志格式定义
- main:日志格式名称
- access_log:日志文件路径
二、基本配置
1、配置server
#user nobody;
worker_processes 1;
#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;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
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; #0拷贝,也就是不会进行用户态和内核态的切换,直接从磁盘读取到内核的存储空间,然后直接返回。
#tcp_nopush on; #快速推送,禁用buffer机制。
#keepalive_timeout 0; #保持的超时时间,减少资源消耗。
keepalive_timeout 65;
#gzip on; #压缩,时间换带宽。
upstream userServices { #配置负载均衡的服务器列表
server 192.168.217.12;
server 192.168.217.13;
}
upstream orderServices { #配置负载均衡的服务器列表
server 192.168.217.12:8080;
server 192.168.217.13:8080;
}
server {
listen 80; #监听的端口
server_name www.darren.com; #服务名
location / { #资源定位
root /mnt;
autoindex on; #自动索引(也就是主页)
}
location /go {
proxy_pass http://192.168.217.12/; #实际请求地址:http://192.168.217.12/
}
location /baidu {
proxy_pass https://www.baidu.com/; #需要将http修改为https,否则会跳转到www.baidu.com
}
location /user {
proxy_pass http://userServices/; #负载均衡到userServices配置的服务器
}
location /order {
proxy_pass http://orderServices/; #负载均衡到orderServices配置的服务器
}
}
server {
listen 80;
server_name node01; #修改为node01
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# }
#}
}
2、windows中的hosts文件配置
3、service nginx reload:重新加载配置
4、!service:history命令从后往前找到匹配命令,然后执行
5、测试
三、yum仓库
1、挂载cdrom目录到mnt
mount /dev/cdrom /mnt
2、配置location
3、测试
四、匹配规则
nginx 收到请求头:判定ip、port、hosts决定server
nginx location匹配:用客户端的uri匹配location的uri
1、先普通
- 顺序无关
- 最大前缀
- 匹配规则简单
打断(不再正则匹配):
- ^~
- 完全匹配
2、再正则
- 不完全匹配
- 正则特殊性:一条URI可以和多条location匹配上
- 有顺序的
- 先匹配,先应用,即时退出匹配。
五、反向代理
1、格式:proxy_pass ip:port[uri]
location /go {
proxy_pass http://192.168.217.12/; #实际请求地址:http://192.168.217.12/
}
如果被代理的地址后面接了uri,这去掉最大前缀,例如:"http://192.168.217.12/"带了"/",则不会拼接"/go"。
2、测试
六、负载均衡
1、配置
upstream userServices { #配置负载均衡的服务器列表
server 192.168.217.12;
server 192.168.217.13;
}
server {
listen 80; #监听的端口
server_name www.darren.com; #服务名
location /user {
proxy_pass http://userServices/; #负载均衡到userServices配置的服务器
}
}