在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解
看了很多nginx的配置,好像都忽略了ip直接访问web的问题,不利于SEO优化,所以我们希望可以避免直接用IP访问网站,而是域名访问,具体怎么做呢,看下面。
官方文档中提供的方法:
If you do not want to process requests with undefined “Host” header lines, you may define a default server that just drops the requests:
server {
listen 80 default_server;
server_name _;
return 444;
}
说白了就是只要是ip访问的直接重置444错误。
但是这样好像又不太友好,如果能直接给跳转到该web server的网址就好了。
配置如下:
server {
listen 80 default_server;
server_name _;
rewrite ^ http://www.domain.com$request_uri?;
}
这样还是有一点问题,某些特别的地址,我需要用ip访问,其他的都禁止,如何配置呢?
比如说我想让监控宝直接用ip访问我的机器的nginx状态信息,其他的用ip访问的所有请求都跳转到域名上。
server {
listen 80 default_server;
server_name _;
location /xxxxx{
stub_status on;
access_log off;
}
location /{
rewrite ^ http://www.nginxs.com$request_uri?;
}
}
这样就实现了我们想要的功能了。
另外,在这里说一下server_name。
server_name 是可以使用正则表达式的,这个功能因该说相当实用。
Nginx中的server_name指令主要用于配置基于名称的虚拟主机,server_name指令在接到请求后的匹配顺序分别为:
1、准确的server_name匹配,例如:
server {
listen 80;
server_name domain.com www.domain.com;
...
}
2、以*通配符开始的字符串:
server {
listen 80;
server_name *.domain.com;
...
}
3、以*通配符结束的字符串:
server {
listen 80;
server_name www.*;
...
}
4、匹配正则表达式:
server {
listen 80;
server_name ~^(?.+)\.domain\.com$;
...
}
server
{
listen 80;
server_name ~^(www\.)?(.+)$;
index index.php index.html;
root /data/wwwsite/$2;
}
站点的主目录应该类似于这样的结构:
/data/wwwsite/domain.com
/data/wwwsite/nginx.org
/data/wwwsite/baidu.com
/data/wwwsite/google.com
这样就可以只使用一个server块来完成多个站点的配置。
2、在一个server块中为一个站点配置多个二级域名。
实际网站目录结构中我们通常会为站点的二级域名独立创建一个目录,同样我们可以使用正则的捕获来实现在一个server块中配置多个二级域名:
server
{
listen 80;
server_name ~^(.+)?\.domain\.com$;
index index.html;
if ($host = domain.com){
rewrite ^ http://www.domain.com permanent;
}
root /data/wwwsite/domain.com/$1/;
}
站点的目录结构应该如下:
/data/wwwsite/domain.com/www/
/data/wwwsite/domain
.com/nginx/
这样访问www.domain.com时root目录为/data/wwwsite/domain.com/www/,nginx.domain.com时为/data/wwwsite/domain.com/nginx/,以此类推。
后面if语句的作用是将domain.com的方位重定向到www.domain.com,这样既解决了网站的主目录访问,又可以增加seo中对www.domain.com的域名权重。
在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解的更多相关文章
- 配置Nginx防止直接用IP訪问Webserver
看了非常多Nginx的配置,好像都忽略了ip直接訪问Web的问题.这样理论上不利于SEO优化,所以我们希望能够避免直接用IP訪问站点.而是域名訪问.详细怎么做呢.看以下. 官方文档中提供的方法: If ...
- nginx中配置proxy_pass
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...
- Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机
Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机: #user nobody; worker_processes 1; #error_log logs/error.log; #err ...
- 14 nginx 中配置 expires缓存提升网站负载
一:nginx 中配置 expires缓存提升网站负载 对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的 ...
- Nginx中配置vue,react项目地址
如题 像以前在Nginx中配置域名解析的时候只需要在conf.d文件夹下添加对应的xx.conf文件(当然了你也可以在nginx.conf)下配置. 如果是以前的老项目只需要在配置文件中server内 ...
- Nginx中配置https中引用http的问题
Nginx中配置https中引用http的问题 遇到问题: 今天公司要在后台增加直播入口,使用腾讯云的实时音视频,要求是必须使用https,在配置完强制跳转https候,发现后台无法上传图片,在浏览器 ...
- 在Eclipse中配置Tomcat时,出现Cannot create a server using the selected type错误
在eclipse中配置Tomcat时,出现Cannot create a server using the selected type错误 原因:Tomcat被删除或者是重新安装,并且安装目录改变了. ...
- Apache 后台服务器(主要处理php及一些功能请求 如:中文url) Nginx 前端服务器(利用它占用系统资源少得优势来处理静态页面大量请求) Lighttpd 图片服务器 总体来说,随着nginx功能得完善将使他成为今后web server得主流。
Apache 后台服务器(主要处理php及一些功能请求 如:中文url) Nginx 前端服务器(利用它占用系统资源少得优势来处理静态页面大量请求) Lighttpd 图片服务器 总体来说,随着ngi ...
- nginx中配置404错误页面的教程
什么是404页面如果网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是NGI ...
随机推荐
- Django初体验(一):自定义表单提交
注:本人使用的Django1.8.3版本进行测试 除了使用Django内置表单,有时往往我们需要自定义表单.对于自定义表单Post方式提交往往会带来由CSRF(跨站请求伪造)产生的错误"CS ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- ArrayList如何实现线程安全
一:使用synchronized关键字,这个大家应该都很熟悉了,不解释了: 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List< ...
- Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2- ...
- java 22 - 11 多线程之模拟电影院售票口售票
使用多线程实现的第二种方式: 首先创建自定义类 public class SellTicket implements Runnable { // 定义100张票 private int ticket ...
- 转:GCC,LLVM,Clang编译器对比
GCC,LLVM,Clang编译器对比 转自: http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html 在XCode中, ...
- Fragment的startActivityForResult详细解决方案
由于要用到Fragment中startActivityForResult,所以一开始就直接用activity.startActivityForResult(intent, 0);发现这样用首先会跳转到 ...
- MysqlHelper 需要重写
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...
- pullRefresh组件配置
mui.init({ pullRefresh:{ container: '#contanier', indicators:false, up:{ height:200, contentinit: '' ...
- c++ 基于wincrypt的DES CBC模式加解密
des.h #pragma once #include <windows.h> #include <atlstr.h> #include <wincrypt.h> ...