nginx+rewrite+proxy+cache基本實驗
阿新 • • 發佈:2018-05-09
NGINX proxy rewrite cache 今天分享下nginx+rewrite+proxy+cache
nginx於apache的區別
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; } } }
**基於用戶密碼控制** [root@DNS-slave conf]# yum install -y httpd-tools [root@DNS-slave conf]# htpasswd -cm /usr/local/nginx/passwd.db boke New password: Re-type new password: Adding password for user boke [root@DNS-slave conf]# cat /usr/local/nginx/passwd.db boke:$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日誌輪訓](http://blog.51cto.com/lookingdream/1794169)
rewrite實驗 [root@DNS-slave 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; [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.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的一個參數沒打開,取消註釋即可)
[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。。
nginx+rewrite+proxy+cache基本實驗