码迷,mamicode.com
首页 > 系统相关 > 详细

nginx+rewrite+proxy+cache基本实验

时间:2018-05-09 19:26:52      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:NGINX   proxy   rewrite   cache   

今天分享下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;
                }
    }

}

技术分享图片
技术分享图片


**基于用户密码控制**

[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基本实验

标签:NGINX   proxy   rewrite   cache   

原文地址:http://blog.51cto.com/13293172/2114556

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!