如何在nginx中为别名别名而不使用别名?

我有我的configuration文件中的这一行

server_name site.com m.site.com 

我的代码检查,如果url是m.site.com并提供移动版本。 但是,当我这样做去m.site.com/abc将显示我的代码site.com/abc这是错误的。 如果我复制/粘贴整个块,并将site.com更改为m.site.com它工作正常。 但我宁愿没有重复的configuration(或代码)。

有没有解决的办法?

即使您在config中检查$http_host ,这也是一种无效的方法 。 所以,最好的方法是把所有常见的位放在一个文件中,并把它包含在单独的server块中:

 server { listen 80; server_name site.com; root /var/www/localhost/htdocs/site.com; include asp.conf } server { listen 80; server_name m.site.com; root /var/www/localhost/htdocs/m.site.com; include asp.conf ... } 

你有两个select:

1)为nginxconfiguration“重复”configuration块

2)让您的站点检测服务器名称(在PHP中,$ _SERVER ['SERVER_NAME']或$ _SERVER ['HTTP_HOST']),并根据它是什么来提供不同的站点。

如果您使用nginx作为反向代理,请尝试在您的proxy_pass指令附近添加proxy_set_header指令,如下所示:

 proxy_pass http://my.asp.net.hostname:8000; proxy_set_header Host $http_host; 

这确保nginx逐字传递主机头,而不是采取主要的server_name。