如果只是针对nginx下的某一个域名进行访问的白名单限制,那么可以在nginx的配置文件里进行设置,利用$remote_addr参数进行访问的分发限制,如下:

    1. [root@china vhosts]# cat testwww.wangshibo.com.conf
    2. server {
    3. listen 80;
    4. server_name testwww.wangshibo.com;
    5. root /var/www/vhosts/testwww.wangshibo.com/httpdocs/main;
    6. access_log /var/www/vhosts/testwww.wangshibo.com/logs/access.log main;
    7. error_log /var/www/vhosts/testwww.wangshibo.com/logs/error.log;
    8. ##白名单设置,只允许下面三个来源ip的客户端以及本地能访问该站。主要是下面这三行
    9. if ($remote_addr !~ ^(100.110.15.16|100.110.15.17|100.110.15.18|127.0.0.1)) {
    10. rewrite ^.*$ /maintence.php last;
    11. }
    12. location / {
    13. try_files $uri $uri/ @router;
    14. index index.php;
    15. }
    16. error_page 500 502 503 504 /50x.html;
    17. location @router {
    18. rewrite ^.*$ /index.php last;
    19. }
    20. location ~ \.php$ {
    21. fastcgi_pass 127.0.0.1:9001;
    22. fastcgi_read_timeout 30;
    23. fastcgi_index index.php;
    24. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    25. #include fastcgi_params;
    26. include fastcgi.conf;
    27. }
    28. }
    29. 错误页面内容设置:
    30. [root@china vhosts]# cat /var/www/vhosts/testwww.wangshibo.com/main/maintence.html
    31. <html>
    32. <head>
    33. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    34. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    35. </head>
    36. <body>
    37. 网站临时维护中,请稍后访问...
    38. </body>
    39. </html>

    也可以使用$http_x_forwarded_for参数进行访问的分发限制,如下:

    1. server {
    2. listen 80;
    3. server_name testwww.wangshibo.com;
    4. root /var/www/vhosts/testwww.wangshibo.com/httpdocs/main;
    5. access_log /var/www/vhosts/testwww.wangshibo.com/logs/access.log main;
    6. error_log /var/www/vhosts/testwww.wangshibo.com/logs/error.log;
    7. ##白名单设置,只允许下面三个来源ip的客户端以及本地能访问该站。
    8. if ($http_x_forwarded_for !~ ^(100.110.15.16|100.110.15.17|100.110.15.18|127.0.0.1)) {
    9. rewrite ^.*$ /maintence.php last;
    10. }
    11. location / {
    12. try_files $uri $uri/ @router;
    13. index index.php;
    14. }
    15. error_page 500 502 503 504 /50x.html;
    16. location @router {
    17. rewrite ^.*$ /index.php last;
    18. }
    19. location ~ \.php$ {
    20. fastcgi_pass 127.0.0.1:9001;
    21. fastcgi_read_timeout 30;
    22. fastcgi_index index.php;
    23. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    24. #include fastcgi_params;
    25. include fastcgi.conf;
    26. }
    27. }

    还可以利用nginx的allow、deny参数进行访问限制

    1. [root@china vhosts]# cat testwww.wangshibo.com.conf
    2. server {
    3. listen 80;
    4. server_name testwww.wangshibo.com;
    5. root /var/www/vhosts/testwww.wangshibo.com/httpdocs/main;
    6. access_log /var/www/vhosts/testwww.wangshibo.com/logs/access.log main;
    7. error_log /var/www/vhosts/testwww.wangshibo.com/logs/error.log;
    8. ##白名单设置,只允许下面三个来源ip的客户端以及本地能访问该站。
    9. allow 100.110.15.16;
    10. allow 100.110.15.17;
    11. allow 100.110.15.18;
    12. allow 127.0.0.1;
    13. deny all;
    14. location / {
    15. try_files $uri $uri/ @router;
    16. index index.php;
    17. }
    18. error_page 500 502 503 504 /50x.html;
    19. location @router {
    20. rewrite ^.*$ /index.php last;
    21. }
    22. location ~ \.php$ {
    23. fastcgi_pass 127.0.0.1:9001;
    24. fastcgi_read_timeout 30;
    25. fastcgi_index index.php;
    26. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    27. #include fastcgi_params;
    28. include fastcgi.conf;
    29. }
    30. }