nginx控制指定ip访问网站
今天公司要求给一台nginx反向代理服务器做ip限制,居然要求在维护时,只要2个ip可以访问网站页面,其他ip只能访问维护页面,我居然想都没想就说可以实现,现在想来真的太大胆了点,好了,经过几小时的琢磨还真搞出来了.
系统:centos 5.5
环境:nginx反向代理,ip是192.168.10.5
后端服务器,ip是192.168.10.150
1.先做好nginx反向代理和后端服务环境
这里就不说怎么做了,大家自己网上去找nginx反向代理是怎么做的吧.
2.在反向代理设置
大家可以看下我的nginx反向代理conf文件:
可以看到我的反向代理指向的10.150的9000端口,然后去/var/www/vhosts/wwwroot设置503内容:
cd /var/www/vhosts/wwwroot
vi 503.html
the is 503!!
只让192.168.10.24和192.168.10.169可以访问后端网站,其他ip都访问503.html.
3.在后端服务器设置
cd /var/www/vhosts
vi index.html
the is 10.150!!
4.重启nginx进行验证
service nginx reload
在192.168.10.24的浏览器上输入http://192.168.10.5:9000/,可以看到是允许访问后端网站的

在192.168.10.19的浏览器上输入http://192.168.10.5:9000/,可以看到是不允许访问后端网站的

好了,收工.
系统:centos 5.5
环境:nginx反向代理,ip是192.168.10.5
后端服务器,ip是192.168.10.150
1.先做好nginx反向代理和后端服务环境
这里就不说怎么做了,大家自己网上去找nginx反向代理是怎么做的吧.
2.在反向代理设置
大家可以看下我的nginx反向代理conf文件:
001 | user nginx nginx; |
002 | worker_processes 1; |
003 | #worker_cpu_affinity 00000001 00000010 00000100 00001000 00001001 00001010 00001100 00010000; |
004 | worker_rlimit_nofile 65535; |
005 |
006 | error_log /var/log/nginx/error.log; |
007 |
008 | pid /var/run/nginx.pid; |
009 |
010 | events { |
011 | use epoll; |
012 | worker_connections 65535; |
013 | } |
014 |
015 | http { |
016 | include mime.types; |
017 | default_type application/octet-stream; |
018 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
019 | '$status $body_bytes_sent "$http_referer" ' |
020 | '"$http_user_agent" "$http_x_forwarded_for"' ; |
021 |
022 | access_log /var/log/nginx/access.log main; |
023 |
024 | server_names_hash_bucket_size 128; |
025 | client_header_buffer_size 4k; |
026 | large_client_header_buffers 4 32k; |
027 | client_body_in_file_only clean; |
028 | client_max_body_size 8m; |
029 | |
030 | #open_file_cache max=10240 inactive=20s; |
031 | #open_file_cache_valid 30s; |
032 | #open_file_cache_min_uses 1; |
033 |
034 | sendfile on; |
035 | tcp_nopush on; |
036 |
037 | keepalive_timeout 60; |
038 | tcp_nodelay on; |
039 | server_tokens off; |
040 |
041 | # fastcgi_connect_timeout 300s; |
042 | # fastcgi_send_timeout 300s; |
043 | # fastcgi_read_timeout 300s; |
044 | # fastcgi_buffer_size 128k; |
045 | # fastcgi_buffers 8 128k;#8 128 |
046 | # fastcgi_busy_buffers_size 256k; |
047 | # fastcgi_temp_file_write_size 256k; |
048 | fastcgi_intercept_errors on; |
049 | |
050 | #hiden php version |
051 | fastcgi_hide_header X-Powered-By; |
052 |
053 | gzip on; |
054 | gzip_min_length 1k; |
055 | gzip_buffers 16 64k; |
056 | gzip_http_version 1.0; |
057 | #gzip_disable "MSIE [1-5]\."; |
058 | gzip_comp_level 4; |
059 | gzip_types text/plain application/x-javascript text/css application/xml image/gif image/jpg image/jpeg image/png; |
060 | #gzip_vary on; |
061 | proxy_hide_header Vary; |
062 |
063 | #limit_zone conlimit $binary_remote_addr 1m; |
064 | #limit_conn conlimit 5; |
065 |
066 | upstream 192.168.10.5 { |
067 | server 192.168.10.150:9000; |
068 | } |
069 |
070 | server { |
071 | listen 9000; |
072 | server_name _; |
073 | root /var/www/vhosts/wwwroot; |
074 | error_page 503 /503.html; |
075 |
076 | location / { |
077 | proxy_pass http://192.168.10.5; |
078 | proxy_set_header Host $host; |
079 | proxy_set_header X-Real-IP $remote_addr; |
080 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
081 | set $fuck 0; |
082 | if ($remote_addr = '192.168.10.24' ) { |
083 | set $fuck 1; |
084 | } |
085 | if ($remote_addr = '192.168.10.169' ) { |
086 | set $fuck 1; |
087 | } |
088 | if ($fuck = 0){ |
089 | return 503; |
090 | } |
091 | } |
092 |
093 | error_page 401 403 404 /503.html; |
094 |
095 | location = /503.html { |
096 | root /var/www/vhosts/wwwroot; |
097 | break ; |
098 | } |
099 |
100 | error_page 500 502 503 504 /50x.html; |
101 | |
102 | location = /50x.html { |
103 | root /var/www/vhosts/error; |
104 | } |
105 | } |
106 | include /etc/nginx/conf.d/*.conf; |
107 | } |
可以看到我的反向代理指向的10.150的9000端口,然后去/var/www/vhosts/wwwroot设置503内容:
cd /var/www/vhosts/wwwroot
vi 503.html
the is 503!!
只让192.168.10.24和192.168.10.169可以访问后端网站,其他ip都访问503.html.
3.在后端服务器设置
cd /var/www/vhosts
vi index.html
the is 10.150!!
4.重启nginx进行验证
service nginx reload
在192.168.10.24的浏览器上输入http://192.168.10.5:9000/,可以看到是允许访问后端网站的

在192.168.10.19的浏览器上输入http://192.168.10.5:9000/,可以看到是不允许访问后端网站的

好了,收工.
附件下载:
nginx指定限制.rar 1.19KB