Ubuntu 为什么Nginx只能在设置虚拟机后运行上述服务器?

Ubuntu 为什么Nginx只能在设置虚拟机后运行上述服务器?,ubuntu,nginx,Ubuntu,Nginx,我在我的/etc/nginx/sites enabled/default文件中写了两个服务器配置。它们指向不同的应用程序。但是在我重新启动我的nginx服务器后,它只能运行上面的服务器 我的操作系统是Ubuntu 14.04 LTS,Nginx版本是1.6.0 我的配置: upstream app { # Path to Unicorn SOCK file, as defined pr

我在我的/etc/nginx/sites enabled/default文件中写了两个服务器配置。它们指向不同的应用程序。但是在我重新启动我的nginx服务器后,它只能运行上面的服务器

我的操作系统是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;                                    
        }                                                           
    } 

您已将两台服务器绑定到同一个listen 80端口(默认HTTP端口),并且两台服务器位于同一服务器名称localhost域上。因此,web服务器无法为两个目标分离HTTP通信

您可以通过以下任一方法解决此问题:

将一个放在另一个端口上,例如listen 81; 将一个放置在购买的域名上,例如server_name my.domain.com; 将两个服务器块合并为一个,例如

location /app1 { ... }
location /app2 { ... }
编辑:如果按给定方式组合两个服务器块,则得到以下配置:

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;
    server_name localhost;

    # first app on sub-directory
    location /app1 {
        root /usr/share/nginx/html/app1/public;
        index index.html index.htm;

        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;
    }

    # second app on sub-directory
    location /app2 {
        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        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;
    }
}
您需要进一步优化配置的大部分信息可以在中的相关模块下找到。最重要的模块是HTTP核心、重写、FastCGI和索引。

非常感谢。关于将两个服务器块合并为一个,我不明白。如果使用我的配置文件,你能告诉我怎么做吗?