nginx下禁止直接以IP访问的方法
时间:2014-09-26 08:54 来源:linux.it.net.cn 作者:it
首先,看下Nginx的默认虚拟主机在用户通过IP访问,或通过未设置的域名访问时,要特别注意的一点,在server的设置里面添加:
lnmp的nginx的配置文件nginx.conf
vi /usr/local/nginx/conf/nginx.conf
复制代码代码示例:
server
{
listen 80 default;
server_name _; //就是这行,注意哦。
return 500;
}
放在默认的server前面即可。
return 500; 也可以更换其他的,比如网址等。
完成配置文件参考:
复制代码代码示例:
server
{
listen 80 default;
server_name _;
return 500;
}
server
{
listen 80;
server_name jbxue.com www.it.net.cn;
index index.html index.htm index.php;
root /var/www/html;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
include /var/www/html/nginx.conf;
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/wwwlogs/access.log access;
修改完毕,重启nginx服务,使配置生效:
复制代码代码示例:
# nginx -t reload
或
# service nginx restart
(责任编辑:IT)
首先,看下Nginx的默认虚拟主机在用户通过IP访问,或通过未设置的域名访问时,要特别注意的一点,在server的设置里面添加:
lnmp的nginx的配置文件nginx.conf
复制代码代码示例:
server
{ listen 80 default; server_name _; //就是这行,注意哦。 return 500; }
放在默认的server前面即可。
完成配置文件参考:
复制代码代码示例:
server
{ listen 80 default; server_name _; return 500; } server { listen 80; server_name jbxue.com www.it.net.cn; index index.html index.htm index.php; root /var/www/html; location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } include /var/www/html/nginx.conf; location /status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /home/wwwlogs/access.log access;
修改完毕,重启nginx服务,使配置生效:
复制代码代码示例:
# nginx -t reload
(责任编辑:IT)或 # service nginx restart |