Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am using nginx as server backend

  • sqlbuddy.example.com - for db manage (php-fpm)

  • example.com - main site (unicorn)

When I go to www.example.com I get sqlbuddy.example.com

How do I get example.com at www.example.com

sqlbuddy

  server {
    listen sqlbuddy.example.com:80;
    client_max_body_size 1G;
    server_name sqlbuddy.example.com;
    keepalive_timeout 5;
    root /home/example/sqlbuddy;
    index index.php;

    location ~ \.php$ {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;

      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
      include fastcgi_params;
    }
  }

example.com

  upstream example_server {
   server unix:/home/example/application/shared/unicorn.sock fail_timeout=0;
  }

  server {
    listen example.com:80;
    client_max_body_size 1G;
    server_name example.com;
    keepalive_timeout 5;
    root /home/example/application/current/public;

    try_files $uri/index.html $uri.html $uri @example_application;

    location @example_application {
        proxy_pass http://example_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /home/example/application/current/public;
    }
  }

default

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
        }

        location /doc {
                root /usr/share;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        location /images {
                root /usr/share;
                autoindex off;
        }
  }
share|improve this question

1 Answer 1

up vote 4 down vote accepted

Change the server_name directive in your example.com container:

server {
    listen example.com:80;
    client_max_body_size 1G;
    server_name example.com www.example.com;
    keepalive_timeout 5;
    root /home/example/application/current/public;

    try_files $uri/index.html $uri.html $uri @example_application;

    location @example_application {
        proxy_pass http://example_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/example/application/current/public;
    }
}

It is even possible to use wildcards or regular expressions as described here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.