1. 程式人生 > >域名泛解析後nginx拒絕未繫結域名

域名泛解析後nginx拒絕未繫結域名

第一種:

 

返回HTTP 狀態碼

 
  1. server {

  2. listen 80 default;

  3. server_name _;

  4. return 403;

  5. }

default表示預設主機


403為Http狀態碼,可根據需要修改


這樣可以遮蔽所有域名(除了你自己繫結的)和IP的訪問。


我們也可以吧return 403;改成重定向,把非法訪問重定向到其他地址

 

第二種:

 

 
  1. server {

  2. listen 80 default;

  3. server_name _;

  4. rewrite ^(.*) http://www.baidu.com permanent;

  5. }

改成重定向,把非法訪問重定向到其他地址

 

例子:如下為Nginx.conf的配置

 
  1. #######################################################################

  2. #

  3. # This is the main Nginx configuration file.

  4. #

  5. # More information about the configuration options is available on

  6. # * the English wiki - http://wiki.nginx.org/Main

  7. # * the Russian documentation - http://sysoev.ru/nginx/

  8. #

  9. #######################################################################

  10.  
  11. #----------------------------------------------------------------------

  12. # Main Module - directives that cover basic functionality

  13. #

  14. # http://wiki.nginx.org/NginxHttpMainModule

  15. #

  16. #----------------------------------------------------------------------

  17.  
  18. user nginx;

  19. #worker_processes 16;

  20. worker_processes 4;

  21.  
  22.  
  23. ######add parameter######

  24. worker_rlimit_nofile 65535;

  25. ######end add ###########

  26.  
  27.  
  28.  
  29. error_log /var/log/nginx/error.log;

  30. #error_log /var/log/nginx/error.log notice;

  31. #error_log /var/log/nginx/error.log info;

  32.  
  33. pid /var/run/nginx.pid;

  34.  
  35.  
  36. #----------------------------------------------------------------------

  37. # Events Module

  38. #

  39. # http://wiki.nginx.org/NginxHttpEventsModule

  40. #

  41. #----------------------------------------------------------------------

  42.  
  43. events {

  44. #####add parameter #####

  45. use epoll;

  46. worker_connections 65535;

  47. #####end add ###########

  48. #worker_connections 1024;

  49. }

  50.  
  51.  
  52. #----------------------------------------------------------------------

  53. # HTTP Core Module

  54. #

  55. # http://wiki.nginx.org/NginxHttpCoreModule

  56. #

  57. #----------------------------------------------------------------------

  58.  
  59. http {

  60. include /etc/nginx/mime.types;

  61. default_type application/octet-stream;

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

  64. '$status $body_bytes_sent "$http_referer" '

  65. '"$http_user_agent" "$http_x_forwarded_for"';

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

  68.  
  69. sendfile on;

  70. tcp_nopush on;

  71. server_names_hash_bucket_size 512;

  72. #keepalive_timeout 0;

  73. keepalive_timeout 300;

  74. tcp_nodelay on; ######add file

  75. fastcgi_connect_timeout 300s;

  76. fastcgi_send_timeout 300s;

  77. fastcgi_read_timeout 300s;

  78. fastcgi_buffer_size 64k;

  79. fastcgi_buffers 4 32k;

  80. fastcgi_busy_buffers_size 64k;

  81. fastcgi_temp_file_write_size 64k;

  82.  
  83.  
  84.  
  85. #gzip on;

  86.  
  87. # Load config files from the /etc/nginx/conf.d directory

  88. # The default server is in conf.d/default.conf

  89. client_header_buffer_size 128k; #####add file

  90. large_client_header_buffers 4 128k; #####add file

  91. server{

  92. listen 80 default;

  93. rewrite ^(.*) http://ifengniu.com permanent;

  94. }

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

  96. include sites-enabled/*;

  97.  
  98. }