nginx使用geoip做区域限制


当前第2页 返回上一页

首先在nginx.conf中加载geoip的库,配置如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

load_module "modules/ngx_http_geoip_module.so";

load_module "modules/ngx_stream_geoip_module.so";

 

user  nginx;

worker_processes  1;

 

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

 

    access_log  /var/log/nginx/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    keepalive_timeout  65;

 

    #gzip  on;

 

    include /etc/nginx/conf.d/*.conf;

}

配置虚拟主机如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

geoip_country /etc/nginx/geoipdat/GeoIP.dat;

geoip_city /etc/nginx/geoipdat/GeoLiteCity.dat;

 

 

server {

    listen       80;

    server_name  localhost;

    location / {

    root /opt;

    if ($geoip_country_code = CN){

        rewrite (.*) /zh$1 break;

    }

        rewrite (.*) /en$1 break;

    }

        error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

 

}

opt目录如下

1

2

3

4

5

6

7

[root@VM_0_15_centos opt]# tree

.

|

└── en

│       └── index.html

└── zh

    └── index.html

以上就是nginx使用geoip做区域限制的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

怎么查看nginx端口被占用

nginx反向代理什么意思

比较讲解http中499状态码和nginx下499错误

nginx反向代理和负载均衡区别

linux下如何停止nginx服务

nginx是web服务器吗

docker启动nginx后无法访问怎么办

nginx php-fpm进程 怎么释放

怎么实现nginx访问

nginx是否在启动

更多相关阅读请进入《nginx》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...