Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx设置禁止IP访问域名 #50

Open
hehongwei44 opened this issue Nov 21, 2015 · 3 comments
Open

Nginx设置禁止IP访问域名 #50

hehongwei44 opened this issue Nov 21, 2015 · 3 comments

Comments

@hehongwei44
Copy link
Owner

假设我有一个域名为www.mysite.com,其绑定的主机IP地址为:120.24.23.22。现在我想禁止使用IP访问,当用IP访问的时候会跳转到带域名那种形式的访问。如何实现呢? 其实很简单的

创建www.mysite.com的虚拟主机

创建过程可以参考我前面的文章,这里直接贴出代码:

server {
    listen 80;
    server_name www.mysite.com;

    access_log /home/wwwlogs/access_nginx.log combined;
    root /home/wwwroot/mysite;
    index index.html index.php;

    location /nginx_status {
          stub_status on;
          access_log off;
          allow 127.0.0.1;
          deny all;
    }

    location ~ .*\.(php|php5)?$ {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        expires 30d;
        access_log off;
    }

    location ~ .*\.(js|css)?$ {
        expires 7d;
        access_log off;
        }
    }

建议一个网站对应一个虚拟主机,虚拟主机最好不要建在nginx.conf配置文件里。

屏蔽IP地址访问的操作步骤

接下来就是进行IP地址禁止访问的操作了,在nginx.conf文件中进行配置。配置信息如下:

server {
       listen 80 default_server;
       server_name _;
       rewrite ^ http://www.rulane.com$request_uri?;
    } 

OK,重启服务器,如果配置没有错误的话,就没什么问题了。当我们通过120.24.23.22访问的时候,会自动跳转到www.mysite.com,这就是我们想要的结果

@hehongwei44
Copy link
Owner Author

@silenceper
Copy link

🍻 🍻 🍻 🍻 🍻 🍻

@hehongwei44
Copy link
Owner Author

@silenceper ❤️❤️❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants