2013-05-06 168 views
1

我的问题类似于here。但在我的情况下,我使用Godaddy公共域名,它指向一个公共地址,并路由到我的服务器,它具有固定的私有IP地址(192.168.0.145)。如果我在nginx.conf中将服务器名称作为domain.com,作品。但是,如果我给本地域或服务器私人IP它的作品,即使我浏览到http://domain.com。我甚至尝试将域名添加到我的主机失败[127.0.0.1 domain_name],但没有运气。我通过网络搜索,但我从来没有看到任何人放置私人IP或localdomain在nginx.conf而不是域地址。我真的需要重定向所有http://domain.namehttp://www.domain.com,但由于我无法放置我的域名,我找不到解决方案。我错过了明显的东西?Nginx重写所有HTTP请求到www

server { 
     listen 80; 
     server_name 192.168.0.145; 
     location/{ 
      proxy_set_header X-Forwarded-Host $host; 
      proxy_set_header X-Forwarded-Server $host; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_pass http://localhost:8080; 
     } 
} 

回答

0

这是一个棘手的解决方案,您可以定义2个服务器和非www重定向到www

server{ 
    server_name domain.com; 
    return 301 $scheme://www.domain.com$request_uri 
} 
server{ 
    server_name www.domain.com; 
    #the rest of your normal config goes here 
} 

所有非www将匹配服务器#1,并且将被定向到与服务器#2相匹配的格式

0

在您的服务器部分

server_name www.domain.com domain.com 192.168.0.145; 

if ($host !~* ^www\.domain\.com$) { 
    rewrite ^(.*)$ http://www.domain.com$1 permanent; 
} 
+0

如果因为http://wiki.nginx.org/IfIsEvil我们可以做到没有使用 – user1595858 2013-05-06 14:43:05

+0

我试过了,我可以看到它转发到www.domain.com,然后我得到页面找不到错误。 – user1595858 2013-05-06 15:02:45

+0

你有www.domain.com的DNS记录吗? – zoonman 2013-05-06 16:52:26

相关问题