ubuntu - Nginx 的虚拟主机为何只识别最上面的 server ?

 银仔-zxy 发布于 2022-10-26 15:16

/etc/nginx/sites-enabled/default 中设置两个 server ,分别指向不同的应用。

重启 Nginx 后却只有写在上面的server中的内容生效,为什么?

注意

现在server中写的server_name是localhost;,还没用域名,访问时用IP地址。

系统及服务器版本

OS: Ubuntu 14.04 LTS
Nginx: 1.6.0


配置
    upstream app {                                            
        # Path to Unicorn SOCK file, as defined previously           
        server unix:/usr/share/nginx/html/app1/sockets/unicorn.app1.sock fail_timeout=0;                                                                                                                                               
    }

    server {                                                    
            listen 80;                                              
            index index.html index.htm;                                                                                                                                                                                                  
            server_name localhost;                                      
            root /usr/share/nginx/html/app1/public;                          
            try_files $uri/index.html $uri @app;                         
            location @app {                                             
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                        
                    proxy_set_header Host $http_host;                           
                    proxy_redirect off;                                        
                    proxy_pass http://app;                                      
            }          

            error_page 404 /404.html;                                   

            # redirect server error pages to the static page /50x.html                                                                                                                                                                    
            error_page 500 502 503 504 /50x.html;                       
            location = /50x.html {                                      
                root /usr/share/nginx/html/app1;                                 
            }                                                           
            client_max_body_size 4G;                                    
            keepalive_timeout 10;                                            
    }                 

    # another virtual host using mix of IP-, name-, and port-based configuration                                                                                                                                                         

    server {                                                    
        listen   80;                                               
        root /usr/share/nginx/html;                                
        index index.php index.html index.htm;                               
        server_name localhost;                                       
        location / {                                               
            try_files $uri $uri/ /index.html;                          
        }                                                          
        error_page 404 /404.html;                                    
        error_page 500 502 503 504 /50x.html;                                                                                                                                                                                            
        location = /50x.html {                                      
            root /usr/share/nginx/html;                                 
        }                                                                                                                                                                                                                                
        # pass the PHP scripts to FastCGI server listening on the php-fpm socket                                                                                                                                                         
        location ~ \.php$ {                                         
            try_files $uri = 404;                                       
            fastcgi_pass unix:/var/run/php5-fpm.sock;                        
            fastcgi_index index.php;                                    
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        
            include fastcgi_params;                                    
        }                                                           
    } 
1 个回答
  • 不同的虚拟主机域名不能相同。起个新的 server_name, 再编辑下 /etc/hosts 加入新hosts的域名绑定。
    建议把不同的 server,分开放在 /etc/nginx/sites-enabled/目录下。

    2022-10-27 00:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有