Nginx相关操作-页面内容替换--跨域访问--访问控制

以下操作是在linux中安装了nginx之后进行的

1、nginx 页面内容替换

1、准备工作
在/usr/share/nginx/html目录下新建一个文件:submodule.html,写入内容:hello world hello world!
2、在地址栏输入:

http://183.170.26.65/submodule.html

可以看到我们的hello world hello world!
2、修改配置文件
找到:

 location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
}

将其修改为:

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        sub_filter  'hello' 'HELLO';
}

3、命令行:

 systemctl reload nginx

刷新刚才的页面:
可以看到我们替换过的内容:HELLO world hello world!
我们会发现只有第一个被替换掉了,这时候,继续修改我们的配置文件:
加上一行:sub_filter_once off;

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        sub_filter  'hello' 'HELLO';
        sub_filter_once off;
}

重新:

 systemctl reload nginx

刷新页面:可以看到,所有hello都被替换成了“HELLO”;

2、Nginx的访问控制

1、基于ip
2、基于身份验证
linux命令:重命名操作:

1、基于ip
 mv default.conf  access_mod.conf

查看自己的ip:a.b.c.d
地址栏输入a.b.c.d/1.html进行页面的访问,例如,我们要对/opt/code/1.html进行访问的控制;
将default.conf 重命名为:access_mod.conf
修改access_mod.conf
复制配置文件中的下一段代码:

 location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
}

修改为:

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
}
location ~ ^/1.html {
        root  /opt/code;
        deny a.b.c.d;
        allow all;
        index  index.html index.htm;
}

重新load:

 nginx -s reload -c /etc/nginx/nginx.conf

重新输入:

http://a.b.c.d/1.html

显示403错误,页面该ip不允许访问。

同样的。如果我们只想让本机ip进行访问,则,将配置文件改为:
location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
}
location ~ ^/1.html {
        root  /opt/code;
        deny all;
        allow a.b.c.d;
        index  index.html index.htm;
}
2、基于身份验证

配置配置文件:
首先安装:

yum install httpd-tools -y

之后创建用户,用户名为qiuqiu:

 htpasswd -c ./auth_conf qiuqiu

会提示:

New password: 
Re-type new password: 

输入密码就行了:

 more ./auth_conf 

可以查看刚才的账号密码信息
然后配置我们的配置文件


   location ~ ^/1.html {
        auth_basic "Auth access test!input your password!";
        auto_basic_user_file /etc/nginx/auth_conf;
        root  /opt/code;
        index  index.html index.htm;
    }

检查有没有错误:

nginx -t -c /etc/nginx/nginx.conf

重载服务:

systemctl reload nginx

重新访问我们的页面:

http://a.b.c.d/1.html

会弹出对话框:身份验证。输入用户名密码后即可进入页面

nginx的跨域访问

默认跨域访问是不允许的,那么什么是跨域访问呢?跨域访问就是在加载一个域名页面的时候发送了一个异步请求加载另外一个域名访问请求,
例如:
在一个域名中的页面:
文件目录:/opt/app/code/two.html

<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.14/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(docunment).ready(function(){
   $.ajax({
      type:"GET",
      url:"http://www.baidu.com",
      success:function(data){
         alert("success")
      },
      error:function(){
         alert("fail");
      }
   });
});
</script>
<body>
 <h1>测试跨域访问</h1>
</body>
</html>

配置文件:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/html;
    }


    location ~ .*\.(htm|html)$  {
        #expires:10s;
        #add_header Access-Control-Allow-Origin 183.170.26.65/two.html;
        #add_header Access-Control-Allow-Methods GET,SET,POST,PUT,DELETE,OPTIONS;
        root   /opt/app/code;
    }


    # 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;
    #}
}

可以看到

 #add_header Access-Control-Allow-Origin jsj.haue.edu.cn    

是被注释掉的
检查是否有错误:

nginx -t -c /etc/nginx/nginx.conf

重新加载:

systemctl reload nginx

在地址栏中输入地址(跟自己情况而定):

http://183.170.26.65/two.html

可以看到:
这里写图片描述
错误信息:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://183.170.26.65' is therefore not allowed access.
然后我们修改配置文件,将注释打开

 location ~ .*\.(htm|html)$  {
        #expires:10s;
        add_header Access-Control-Allow-Origin 183.170.26.65/two.html;
        add_header Access-Control-Allow-Methods GET,SET,POST,PUT,DELETE,OPTIONS;
        root   /opt/app/code;
    }

重新load:

systemctl restart nginx.service

这里写图片描述
成功访问!


注意:跨域访问,在配置文件中写的是在nginx所在的主机上
add_header Access-Control-Allow-Origin 183.170.26.65/two.html;
这句话的意思是开启“183.170.65/two.html”的跨域访问功能,这个地址写的而必须是nginx本机上的页面,而不是别的域名或页面。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值