今天分享下nginx+rewrite+proxy+cache
nginx于apache的区别
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;
}
}
}
**基于用户密码控制**
[[email protected] conf]# yum install -y httpd-tools
[[email protected] conf]# htpasswd -cm /usr/local/nginx/passwd.db boke
New password:
Re-type new password:
Adding password for user boke
[[email protected] conf]# cat /usr/local/nginx/passwd.db
boke:$apr1$pSOqFIOj$2LLFh0gedT5fEEkx5WbfD.
[[email protected] conf]# vim nginx.conf
server_name www.abc.com;
location / {
auth_basic "请输入用户名和密码";
auth_basic_user_file "/usr/local/nginx/passwd.db";
[[email protected] conf]# restartnx (这个是个alias)
alias restartnx=‘/usr/local/nginx/sbin/nginx -s reload‘
[nginx日志轮训](http://blog.51cto.com/lookingdream/1794169)
rewrite实验
[[email protected] conf]# vim nginx.conf
server {
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;
[[email protected] conf]# tree /usr/local/nginx/html/joy/
/usr/local/nginx/html/joy/
├── boke
│?? ├── 1.html
│?? ├── 2.html
│?? └── index.html
└── index.html
# proxy反向代理
[[email protected] conf]# vim nginx.conf
server {
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-master
httpd: 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的一个参数没打开,取消注释即可)
[[email protected] 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可以使用缓存,减轻后端服务器压力
[[email protected] nginx]# vim nginx.conf (主配置文件)
虚拟主机配置
下次分享lnmp。。
原文地址:http://blog.51cto.com/13293172/2114556
时间: 2024-11-07 20:02:28