nginx $server_name 始终是本地主机

标签 nginx amazon-ec2 localhost server-name

我有一个在 AWS EC2 实例上运行的 nginx 1.4.5 服务器。我有两个 conf 文件:default.confmyapp.confdefault.conf 监听 localhost,myapp.conf 监听 myapp.mydomain.com

但是我发现即使我在浏览器中输入 myapp.mydomain.com,它总是会加载 default.conf。我更改了 nginx.conf 文件中的 log_format 添加了一个 "$server_name" 字段来检查每个请求的服务器名称。我发现它总是 localhost

有人对此有任何想法吗?

配置信息更新

nginx.conf

server {
     listen       80;
     server_name  localhost;

     #charset koi8-r;
     #access_log  /var/log/nginx/log/host.access.log  main;

     location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
     }
...
}

myapp.conf

 server {
     listen 80;
     server_name myapp.mydomain.com;

     access_log /var/log/nginx/myapp.log;

     location / {
         add_header P3P 'CP="CAO PSA OUR"';
         proxy_pass http://127.0.0.1:8080/myapp/;
         set $ssl off;
         if ($scheme = https) {
             set $ssl on;
         }
         proxy_set_header X-Forwarded-Ssl $ssl;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
     }
 }

是的,我在两道防火墙后面,一道是 iptable,另一道是 AWS 内置的安全组

最佳答案

有人拥有 similar problem通过以下方式修复:

I've fixed this now, in case anyone has the same problem as me it was to do with "server_names_hash_bucket_size 64;" in /etc/nginx/nginx.conf.

That line was commented out by default so I uncommented and restarted nginx and it works fine now.

关于nginx $server_name 始终是本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22157608/

相关文章:

node.js - WebSocket 安全本地主机连接

linux - 使用自定义虚拟主机在 docker 中运行 nginx

amazon-web-services - AWS 的动态 SSL 证书

.htaccess - 将 Apache 转换为 Nginx

amazon-web-services - CloudFormation Windows 实例引导

javascript - Opera 26 (chromium) 中的 getUserMedia 来自本地主机被阻止

linux - Docker 上的 nginx: [emerg] socket() 0.0.0.0:80 失败(13:权限被拒绝)

ubuntu - 如何将 SSL 证书用于在同一服务器实例中运行的两个端口

amazon-web-services - AWS AMI : RegisterImage vs CreateImage

php - 如何为本地主机正确下载并安装带有 PHP、MySQL 等的 apache?