2013-07-10 7 views
30

मैं Nginx के लिए नया हूं और मैं सबडोमेन काम करने की कोशिश कर रहा हूं।nginx - दो सबडोमेन कॉन्फ़िगरेशन

मैं करना चाहते हैं क्या मेरे डोमेन लेने और जोड़ने (यह example.com कॉल):

  • sub1.example.com,
  • sub2.example.com, और भी
  • www.example.com उपलब्ध है।

मुझे पता है कि अपाचे के साथ ऐसा कैसे करें, लेकिन Nginx असली सिर स्क्रैचर है।

मैं डेबियन 6.

मेरे वर्तमान /etc/nginx/sites-enabled/example.com चल रहा हूँ:

server { 
    server_name www.example.com example.com; 
    access_log /srv/www/www.example.com/logs/access.log; 
    error_log /srv/www/www.example.com/logs/error.log; 
    root /srv/www/www.example.com/public_html; 

    location/{ 
     index index.html index.htm; 
    } 

    location ~ \.php$ { 
     include /etc/nginx/fastcgi_params; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name; 
    } 
} 

यह example.com और www.example सेवा करने के लिए काम कर रहा है। कॉम।

server { 
     server_name www.example.com example.com; 
     access_log /srv/www/www.example.com/logs/access.log; 
     error_log /srv/www/www.example.com/logs/error.log; 
     root /srv/www/www.example.com/public_html; 

     server { 
      server_name sub1.example.com; 
      access_log /srv/www/example.com/logs/sub1-access.log; 
      error_log /srv/www/example.com/logs/sub1-error.log; 
      root /srv/www/example.com/sub1; 
    } 
     location/{ 
      index index.html index.htm; 
     } 

     location ~ \.php$ { 
      include /etc/nginx/fastcgi_params; 
      fastcgi_pass 127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name; 
     } 
} 

कोई भाग्य:

मैं एक ही फाइल में एक दूसरे सर्वर ब्लॉक जोड़ने के लिए की तरह की कोशिश की है। कोई विचार? मैं किसी भी प्रतिक्रिया की सराहना करता हूं।

+0

मुझे उल्लेख करना चाहिए था: example.com/sub2 पर जाने के लिए example.com/sub1 और sub2.example.com पर जाने के लिए अंतिम लक्ष्य sub1.example.com के लिए है। मुझे उम्मीद है कि इसका कोई अर्थ है। – boredemt

उत्तर

43

गलती एक सर्वर ब्लॉक के अंदर एक सर्वर ब्लॉक डाल रहा है, आप मुख्य सर्वर ब्लॉक तो उप डोमेन

server { 
    server_name example.com; 
    # the rest of the config 
} 
server { 
    server_name sub1.example.com; 
    # sub1 config 
} 
server { 
    server_name sub2.example.com; 
    # sub2 config 
} 
4

तुम बस के स्थान पर निम्न पंक्ति जोड़ने की जरूरत के लिए एक नया एक खोलने बंद हो जाना चाहिए आपका server_name

server_name xyz.com *.xyz.com; 

और Nginx को पुनरारंभ करें। बस।

+9

क्या होगा यदि आप चाहते हैं कि सबडोमेन सर्वर पर एक अलग संसाधन को इंगित करे, स्थान www.example.com के संसाधन के मुकाबले? – courtyen