💡 需求:
1.国内用户访问英德文站跳转统一跳转zk.cn
2.国外用户访问固定站 en.zk.com/de.zk.com ( 未作调整)

模块 —with-http_geoip_module
官网 https://dev.maxmind.com/

1.编译安装Nginx

参考:https://www.cnblogs.com/clsn/p/7750615.html

2.Nginx配置

  1. #user nobody;
  2. worker_processes 1;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  10. '$status $body_bytes_sent "$http_referer" '
  11. '"$http_user_agent" "$http_x_forwarded_for"';
  12. access_log logs/access.log main;
  13. sendfile on;
  14. keepalive_timeout 65;
  15. #gzip on;
  16. geoip_country /server/tools/nginx-1.20.1/geoip/GeoIP.dat;
  17. geoip_city /server/tools/nginx-1.20.1/geoip/GeoLiteCity.dat;
  18. ##############################################################测试 #################################################################
  19. server {
  20. listen 81;
  21. server_name localhost;
  22. root /server/tools/nginx-1.20.1/html;
  23. #charset koi8-r;
  24. #access_log logs/host.access.log main;
  25. location / {
  26. if ($geoip_country_code = CN) {
  27. root /server/tools/nginx-1.20.1/html;
  28. }
  29. if ($geoip_country_code != CN) {
  30. root /server/tools/nginx-1.20.1/html/test;
  31. }
  32. }
  33. }
  34. ##############################################################英文 官网#################################################################
  35. server {
  36. listen 443 ssl;
  37. server_name www.zk.com en.zk.com;
  38. root /ftpshare;
  39. index index.php index.html;
  40. ssl_certificate /server/tools/nginx-1.20.1/ssl/6543710__zk.com.pem;
  41. ssl_certificate_key /server/tools/nginx-1.20.1/ssl/6543710__zk.com.key;
  42. ssl_session_timeout 5m; #缓存有效期
  43. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
  44. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
  45. ssl_prefer_server_ciphers on; #使用服务器端的首选算法
  46. location / {
  47. root /ftpshare;
  48. if (!-e $request_filename){
  49. rewrite ^(.*)$ /index.php?s=$1 last;
  50. break;
  51. }
  52. if ($geoip_country_code = CN) {
  53. rewrite ^/(.*) https://www.zk.cn;
  54. }
  55. }
  56. location ~* .*\.(jpg|gif|png)$ {
  57. root /ftpshare;
  58. gzip on;
  59. gzip_http_version 1.1;
  60. gzip_comp_level 2;
  61. gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  62. }
  63. location ~ .*\.(jpg||jpeg|gif|png)$ {
  64. valid_referers none blocked test.zk.com;
  65. if ($invalid_referer) {
  66. return 403;
  67. }
  68. }
  69. location ~ \.php$ {
  70. root /ftpshare;
  71. index index.php index.html;
  72. fastcgi_pass 127.0.0.1:9000;
  73. fastcgi_index index.php;
  74. fastcgi_param SCRIPT_FILENAME /ftpshare$fastcgi_script_name;
  75. include fastcgi_params;
  76. }
  77. }
  78. server {
  79. listen 80;
  80. server_name www.zk.com zk.com;
  81. return 301 https://$server_name$request_uri;
  82. }
  83. ##############################################################德文 官网#################################################################
  84. server {
  85. listen 443 ssl;
  86. server_name de.zk.com;
  87. root /usr/share/nginx/html;
  88. index index.php index.html;
  89. ssl_certificate /server/tools/nginx-1.20.1/ssl/6543710__zk.com.pem;
  90. ssl_certificate_key /server/tools/nginx-1.20.1/ssl/6543710__zk.com.key;
  91. ssl_session_timeout 5m; #缓存有效期
  92. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
  93. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
  94. ssl_prefer_server_ciphers on; #使用服务器端的首选算法
  95. location / {
  96. root /usr/share/nginx/html;
  97. if (!-e $request_filename){
  98. rewrite ^(.*)$ /index.php?s=$1 last;
  99. break;
  100. }
  101. if ($geoip_country_code = CN) {
  102. rewrite ^/(.*) https://www.zk.cn;
  103. }
  104. }
  105. location ~* .*\.(jpg|gif|png)$ {
  106. root /usr/share/nginx/html;
  107. gzip on;
  108. gzip_http_version 1.1;
  109. gzip_comp_level 2;
  110. gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  111. }
  112. location ~ .*\.(jpg||jpeg|gif|png)$ {
  113. valid_referers none blocked test.zk.com;
  114. if ($invalid_referer) {
  115. return 403;
  116. }
  117. }
  118. location ~ \.php$ {
  119. root /usr/share/nginx/html;
  120. index index.php index.html;
  121. fastcgi_pass 127.0.0.1:9000;
  122. fastcgi_index index.php;
  123. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  124. include fastcgi_params;
  125. }
  126. }
  127. } #http区块

3.相关工具

GeoIP.datGeoLiteCity.dat