简体   繁体   English

nginx反向代理重定向到错误的域

[英]nginx reverse proxy redirecting to wrong domain

I'm using this tutorial nginx reverse proxy tutorial to setup a node site with nginx. 我正在使用本教程nginx反向代理教程来使用nginx设置节点站点。 this is what my nano /etc/nginx/conf.d/mydomain.com.conf looks like 这就是我的nano /etc/nginx/conf.d/mydomain.com.conf的样子

server { 
listen 80;
server_name mydomain.com;
location / { 
proxy_pass http://localhost:3000; 
proxy_http_version 1.1; 
proxy_set_header Upgrade $http_upgrade; 
proxy_set_header Connection 'upgrade'; 
proxy_set_header Host $host; 
proxy_cache_bypass $http_upgrade; 
} 
}

The problem is that when I visit my domain, it's redirecting to another domain that I have setup on the same server. 问题是,当我访问我的域时,它会重定向到我在同一服务器上设置的另一个域。 I setup that other domain (a static page) using an nginx virtual hosts tutorial that uses server blocks. 我使用使用服务器块的Nginx虚拟主机教程来设置其他域(静态页面)。 One difference I noticed is that the nginx reverse proxy tutorial doesn't do any of this symlinking between sites available and sites enabled which the virtual hosts tutorial does with the server blocks files. 我注意到的一个区别是,nginx反向代理教程在可用站点和启用的站点之间没有进行任何这种符号链接,虚拟主机教程使用服务器阻止文件来进行此链接。 The virtual hosts tutorial instructs the reader to create server block files and then enable them like this 虚拟主机教程指导读者创建服务器阻止文件,然后像这样启用它们

sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enabled/demo

Do I have to do anything to enable the config file when setting up a reverse proxy with nginx? 使用Nginx设置反向代理时,我需要做任何事情来启用配置文件吗? If not, do you know why it's redirecting to the other domain? 如果不是,您知道为什么它重定向到另一个域吗?

/etc/nginx/conf.d/mydomain.com.conf

Let me start with a small explanation on how nginx matches the hosts, quoting from how nginx processes a request 让我先从nginx 如何处理请求的角度出发,对nginx如何与主机进行匹配进行简要说明

In this configuration nginx tests only the request's header field “Host” to determine which server the request should be routed to. 在此配置中,nginx仅测试请求的标头字段“主机”以确定应将请求路由到哪个服务器。 If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port. 如果其值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,则nginx会将请求路由到该端口的默认服务器。

According to your description I would say there's 2 possibilities, 根据您的描述,我会说有两种可能性,

  1. either that this reverse proxy virtual host has a wrong name, so it's not matched and the request is directed to the first virtual host that listens on port 80. 该反向代理虚拟主机的名称错误,因此不匹配,该请求将定向到侦听端口80的第一台虚拟主机。
  2. the reverse proxy is correct but the configuration was not loaded. 反向代理正确,但未加载配置。

To fix this double check that this line is correct server_name mydomain.com; 要解决此问题,请仔细检查此行是否正确server_name mydomain.com; and indeed matches the URL you are requesting, then make sure you reloaded nginx settings sudo service nginx reload 并确实与您请求的URL匹配,然后确保您重新加载了nginx设置sudo service nginx reload

问题是/etc/nginx/conf.d/mydomain.com.conf没有被复制到

/etc/nginx/sites-enabled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM