Serving domain and sub-domain with Nginx and php-fpm

268

Basically I need to logically separate 2 sites on one hosting and one domain name registered. Firstly, I've set up a sub-domain using CNAME record. Then I've created 2 files for domain and sub-domain (no syntax errors, nginx restarted after changes):

subdomain.conf

server {
listen 80;
server_name sub.domain.com;
index index.php index.html;
root /web/www/sub.domain.com;
access_log   off;
server_tokens off;

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/webserv/var/php-cgi.sock;
    fastcgi_index index.php;
    include /webserv/nginx/conf/fastcgi.conf;}}

domain.conf

server {
listen 80;
server_name domain.com;
index index.php index.html;
root /web/www/domain.com;
access_log   off;
server_tokens off;

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/webserv/var/php-cgi.sock;
    fastcgi_index index.php;
    include /webserv/nginx/conf/fastcgi.conf;}}

But only one of them is working, and a request in browser to the other doesn't even get to nginx, and eventually it says the page load time was too long. What is the easiest way of doing this? Should several instances of nginx, php-fpm be used?

550

Answer

Solution:

Your configuration looks fine, if the seconde file was enabled and wrong nginx would have triggered a server error, but from what you are saying the problem come from your domain name, maybe it is missconfigured or has not yet been propagated.

621

Answer

Solution:

Resolved, it wasn't working as I was using google public DNS on local machine(who would have thought)

People are also looking for solutions to the problem: php - mod_rewrite rule works on localhost but not on production server, due to presence of file extension

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.