本文共 3712 字,大约阅读时间需要 12 分钟。
今天分享下nginx+rewrite+proxy+cache
1.nginx基于ip做限制
小实验:worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.abc.com; location / { root html/abc; index index.html index.htm; } location ~ \.doc$ { (访问以doc结尾的文件只允许13这台机器,顺序控制。) root html/abc; allow 192.168.10.13; deny all; } }}
**基于用户密码控制**[root@DNS-slave conf]# yum install -y httpd-tools[root@DNS-slave conf]# htpasswd -cm /usr/local/nginx/passwd.db bokeNew password:Re-type new password:Adding password for user boke[root@DNS-slave conf]# cat /usr/local/nginx/passwd.dbboke:$apr1$pSOqFIOj$2LLFh0gedT5fEEkx5WbfD.[root@DNS-slave conf]# vim nginx.conf server_name www.abc.com; location / { auth_basic "请输入用户名和密码"; auth_basic_user_file "/usr/local/nginx/passwd.db"; [root@DNS-slave conf]# restartnx (这个是个alias) alias restartnx='/usr/local/nginx/sbin/nginx -s reload'[nginx日志轮训](https://blog.51cto.com/lookingdream/1794169)
rewrite实验[root@DNS-slave conf]# vim nginx.confserver { listen 80; server_name www.joy.com; location / { charset utf-8; root html/joy; index index.html index.htm; location ~ /boke/.* { rewrite ^/boke/.* /boke/index.html break; [root@DNS-slave conf]# tree /usr/local/nginx/html/joy//usr/local/nginx/html/joy/├── boke│ ├── 1.html│ ├── 2.html│ └── index.html└── index.html
# proxy反向代理[root@DNS-slave conf]# vim nginx.confserver { listen 80; server_name www.joy.com; location / { proxy_pass http://192.168.10.15; (访问www.joy.com的就转到后端服务器的ip上,但是这样做会带来问题?多个URL怎么访问?) } }使用nginx自带的变量完成。server { listen 80; server_name www.joy.com; location / { proxy_pass http://192.168.10.15;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;#proxy_set_header X-Forwarded-For $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }server { listen 80; server_name www.sss.com; location / { proxy_pass http://192.168.10.15; proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;#proxy_set_header X-Forwarded-For $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }}192.168.10.15web服务器配置两个虚拟主机Starting httpd: httpd: apr_sockaddr_info_get() failed for DNS-masterhttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName[Wed May 09 02:30:26 2018] [warn] _default_ VirtualHost overlap on port 80, the first has precedence [ OK ](启动时报错,httpd.conf的一个参数没打开,取消注释即可)[root@DNS-master conf]# vim httpd.conf # Use name-based virtual hosting. #NameVirtualHost *:80
轮训策略默认为1:1侧重轮训 绑定轮训 这里使用默认轮训upstream apache_pool {server 192.168.10.15;server 192.168.10.14;}server { listen 80; server_name www.joy.com; location / { proxy_pass http://apache_pool; (这里写的是地址池)proxy_set_header Host $host;
nginx缓存
nginx可以使用缓存,减轻后端服务器压力[root@DNS-slave nginx]# vim nginx.conf (主配置文件)
虚拟主机配置
下次分享lnmp。。
转载于:https://blog.51cto.com/13293172/2114556