nginx – 仅允许某些国家/地区的流量访问特定页面

前端之家收集整理的这篇文章主要介绍了nginx – 仅允许某些国家/地区的流量访问特定页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用–with-http_geoip_module来识别流量.有些页面我只想让某个国家/地区访问.这是配置:

对于http

  1. http{
  2. geoip_country /usr/share/GeoIP/GeoIP.dat; # the country IP database
  3. map $geoip_country_code $allowed_country {
  4. default 0;
  5. US 1;
  6. UK 1;
  7. HK 1;
  8. }
  9. }

位置指令:

  1. location = /testing {
  2. if ($allowed_country = 0) {
  3. return 301 ;
  4. }
  5. }

问题是当我使用美国/香港IP时,我收到404错误.我做错了什么?

UPDATE

这是我完整的conf文件

  1. http {
  2. include /etc/Nginx/mime.types;
  3. default_type application/octet-stream;
  4. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. '$status $body_bytes_sent "$http_referer" '
  6. '"$http_user_agent" "$http_x_forwarded_for"';
  7. access_log /var/log/Nginx/access.log main;
  8. geoip_country /usr/share/GeoIP/GeoIP.dat; # the country IP database
  9. map $geoip_country_code $allowed_country {
  10. default 0;
  11. US 1;
  12. UK 1;
  13. HK 1;
  14. }
  15. sendfile on;
  16. #tcp_nopush on;
  17. #keepalive_timeout 0;
  18. keepalive_timeout 65;
  19. #gzip on;
  20. index index.html index.htm;
  21. # Load modular configuration files from the /etc/Nginx/conf.d directory.
  22. # See http://Nginx.org/en/docs/ngx_core_module.html#include
  23. # for more information.
  24. include /etc/Nginx/conf.d/*.conf;
  25. server {
  26. listen 80 default_server;
  27. server_name localhost;
  28. root /var/www/html;
  29. include /etc/Nginx/default.d/*.conf;
  30. location / {
  31. index index.PHP index.cgi index.pl index.html index.xhtml index.htm index.shtml;
  32. try_files $uri $uri/ /index.PHP?$args;
  33. }
  34. # redirect server error pages to the static page /40x.html
  35. #
  36. error_page 404 /404.html;
  37. location = /40x.html {
  38. }
  39. # redirect server error pages to the static page /50x.html
  40. #
  41. error_page 500 502 503 504 /50x.html;
  42. location = /50x.html {
  43. }
  44. #ban specifc country
  45. location = /testing {
  46. if ($allowed_country = 0) {
  47. return 301 ;
  48. }
  49. }
  50. location ~ \.PHP${
  51. ### SET GEOIP Variables ###
  52. fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
  53. fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
  54. fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
  55. fastcgi_split_path_info ^(.+?\.PHP)(/.*)$;
  56. if (!-f $document_root$fastcgi_script_name) {
  57. return 404;
  58. }
  59. try_files $uri $uri/ /index.PHP?$args;
  60. fastcgi_pass unix:/run/PHP-fpm/PHP-fpm.sock;
  61. fastcgi_index index.PHP;
  62. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  63. include fastcgi.conf;
  64. include conf/site.conf;
  65. }
  66. }
  67. }

基本上我只想访问美国,英国和香港的访问/测试页面.

最佳答案
我们从一开始就这样做.由于您没有说明您的操作系统是什么,因此Debian / Ubuntu和CentOS / Fedora / RHEL的所有步骤都将分开.

1.连接到服务器:

首先,通过终端/控制台(在linux中 – ssh username @ server_ip)或Putty(在windows中)连接到您的服务器.

2.验证GEOIP模块:

由于您已经安装了Nginx,请检查它是否使用HttpGeoipModule进行编译:

CentOS / Fedora / RHEL和Debian / Ubuntu:

  1. Nginx -V

然后尝试找到–with-http_geoip_module.如果它存在则可以继续,否则意味着您没有使用GeoIP模块编译Nginx.

3.安装GEOIP数据库

于Debian / Ubuntu:

  1. sudo apt-get install geoip-database libgeoip1

CentOS的/ Fedora的/ RHEL:

它位于EPEL存储库中,因此您应首先启用它:

CENTOS 4:

32位:

  1. rpm -Uvh http://download.fedoraproject.org/pub/epel/4/i386/epel-release-4-10.noarch.rpm
  2. rpm Uvh http://rpms.famillecollet.com/enterprise/remi-release-4.rpm

64位:

  1. rpm -Uvh http://download.fedoraproject.org/pub/epel/4/x86_64/epel-release-4-10.noarch.rpm
  2. rpm Uvh http://rpms.famillecollet.com/enterprise/remi-release-4.rpm

CENTOS 5:

32位:

  1. rpm -Uvh http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
  2. rpm Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

64位:

  1. rpm -Uvh http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
  2. rpm Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

CENTOS 6:

32位:

  1. rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
  2. rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

64位:

  1. rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
  2. rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

CENTOS 7:

64位:

  1. rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
  2. rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

然后:

  1. yum install geoip geoip-devel -y

4.更新GeoIP数据库

安装GeoIP模块后,数据库将存储在/usr/share/GeoIP/GeoIP.dat中,但可能已过时.那么,让我们更新:

  1. mv /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoIP.dat_bk
  2. cd /usr/share/GeoIP/
  3. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
  4. gunzip GeoIP.dat.gz

或者,您也可以从http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz手动下载数据库,在您的计算机上解压缩并上传到/usr/share / GeoIP / as GeoIP.dat.如果你想在这里做,请不要忘记备份旧的GeoIP.dat.

5.使用GeoIP配置Nginx

打开/etc/Nginx/Nginx.conf(Ubuntu / Debian)或/etc/Nginx/conf/Nginx.conf(CentOS / Fedora / RHEL)并将其放在http {}中,然后再包含:

  1. geoip_country /usr/share/GeoIP/GeoIP.dat;
  2. map $geoip_country_code $allowed_country {
  3. default no;
  4. US yes;
  5. UK yes;
  6. HK yes;
  7. }

这不会阻止国家.我们只设置$allowed_country.

现在,请打开虚拟主机进行配置(/etc/Nginx/conf.d/YOURDOMAINHERE.conf) – 将其置于服务器{}内:

  1. location /testing/ {
  2. if ($allowed_country = no) {
  3. return 403;
  4. }
  5. }

/ testing /是您的网站路径,可从美国,英国和香港访问.

6.重新启动Nginx

  1. /etc/init.d/Nginx reload

它在CentOS和Debian VPS上都经过测试,它正在运行.

希望这会帮助你.

猜你在找的Nginx相关文章