NGINX - Pointing a subdomain to a different directory that is not a subdirectory of the domain directory












2















I am using Nginx as a reverse proxy with Apache. I have a domain, let's say example.com that is pointing at the /var/www/html/example directory.



I want to point a sub-domain phpmyadmin.example.com to a directory /var/www/html/phpmyadmin that is clearly not a sub-directory of the domain's directory.



My complete configuration file /etc/nginx/sites-enabled/example is shown below. I added the parts between ##### BEGIN .. and ###### END .. to my configuration file to try to make this work.



Then I did sudo service nginx restart to restart NGINX successfully but I still cannot access phpmyadmin.example.com.



Here's my configuration:



server {
root /var/www/html/example;

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name example.com;

access_log /var/www/html/example/access.log;
error_log /var/www/html/example/error.log;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

##### BEGIN PHPMYADMIN CONFIG #####
server {
root /var/www/html/phpmyadmin;

index index.php index.html index.htm index.nginx-debian.html;

server_name phpmyadmin.example.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
###### END OF PHPMYADMIN CONFIG #####

server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80 ;
listen [::]:80 ;

server_name example.com www.example.com
return 404; # managed by Certbot
}









share|improve this question























  • your config seems right. but sites-enabled is not the place to configure domains, so sites-available is.

    – rüff0
    Nov 28 '18 at 3:19













  • What happens when you access http://phpmyadmin.example.com?

    – Richard Smith
    Nov 28 '18 at 9:21











  • I get this error message: Hmm. We’re having trouble finding that site. We can’t connect to the server at phpmyadmin.example.com

    – Rahul
    Nov 28 '18 at 15:35
















2















I am using Nginx as a reverse proxy with Apache. I have a domain, let's say example.com that is pointing at the /var/www/html/example directory.



I want to point a sub-domain phpmyadmin.example.com to a directory /var/www/html/phpmyadmin that is clearly not a sub-directory of the domain's directory.



My complete configuration file /etc/nginx/sites-enabled/example is shown below. I added the parts between ##### BEGIN .. and ###### END .. to my configuration file to try to make this work.



Then I did sudo service nginx restart to restart NGINX successfully but I still cannot access phpmyadmin.example.com.



Here's my configuration:



server {
root /var/www/html/example;

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name example.com;

access_log /var/www/html/example/access.log;
error_log /var/www/html/example/error.log;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

##### BEGIN PHPMYADMIN CONFIG #####
server {
root /var/www/html/phpmyadmin;

index index.php index.html index.htm index.nginx-debian.html;

server_name phpmyadmin.example.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
###### END OF PHPMYADMIN CONFIG #####

server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80 ;
listen [::]:80 ;

server_name example.com www.example.com
return 404; # managed by Certbot
}









share|improve this question























  • your config seems right. but sites-enabled is not the place to configure domains, so sites-available is.

    – rüff0
    Nov 28 '18 at 3:19













  • What happens when you access http://phpmyadmin.example.com?

    – Richard Smith
    Nov 28 '18 at 9:21











  • I get this error message: Hmm. We’re having trouble finding that site. We can’t connect to the server at phpmyadmin.example.com

    – Rahul
    Nov 28 '18 at 15:35














2












2








2








I am using Nginx as a reverse proxy with Apache. I have a domain, let's say example.com that is pointing at the /var/www/html/example directory.



I want to point a sub-domain phpmyadmin.example.com to a directory /var/www/html/phpmyadmin that is clearly not a sub-directory of the domain's directory.



My complete configuration file /etc/nginx/sites-enabled/example is shown below. I added the parts between ##### BEGIN .. and ###### END .. to my configuration file to try to make this work.



Then I did sudo service nginx restart to restart NGINX successfully but I still cannot access phpmyadmin.example.com.



Here's my configuration:



server {
root /var/www/html/example;

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name example.com;

access_log /var/www/html/example/access.log;
error_log /var/www/html/example/error.log;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

##### BEGIN PHPMYADMIN CONFIG #####
server {
root /var/www/html/phpmyadmin;

index index.php index.html index.htm index.nginx-debian.html;

server_name phpmyadmin.example.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
###### END OF PHPMYADMIN CONFIG #####

server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80 ;
listen [::]:80 ;

server_name example.com www.example.com
return 404; # managed by Certbot
}









share|improve this question














I am using Nginx as a reverse proxy with Apache. I have a domain, let's say example.com that is pointing at the /var/www/html/example directory.



I want to point a sub-domain phpmyadmin.example.com to a directory /var/www/html/phpmyadmin that is clearly not a sub-directory of the domain's directory.



My complete configuration file /etc/nginx/sites-enabled/example is shown below. I added the parts between ##### BEGIN .. and ###### END .. to my configuration file to try to make this work.



Then I did sudo service nginx restart to restart NGINX successfully but I still cannot access phpmyadmin.example.com.



Here's my configuration:



server {
root /var/www/html/example;

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name example.com;

access_log /var/www/html/example/access.log;
error_log /var/www/html/example/error.log;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

##### BEGIN PHPMYADMIN CONFIG #####
server {
root /var/www/html/phpmyadmin;

index index.php index.html index.htm index.nginx-debian.html;

server_name phpmyadmin.example.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}

# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
###### END OF PHPMYADMIN CONFIG #####

server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80 ;
listen [::]:80 ;

server_name example.com www.example.com
return 404; # managed by Certbot
}






php nginx phpmyadmin reverse-proxy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 2:20









RahulRahul

1469




1469













  • your config seems right. but sites-enabled is not the place to configure domains, so sites-available is.

    – rüff0
    Nov 28 '18 at 3:19













  • What happens when you access http://phpmyadmin.example.com?

    – Richard Smith
    Nov 28 '18 at 9:21











  • I get this error message: Hmm. We’re having trouble finding that site. We can’t connect to the server at phpmyadmin.example.com

    – Rahul
    Nov 28 '18 at 15:35



















  • your config seems right. but sites-enabled is not the place to configure domains, so sites-available is.

    – rüff0
    Nov 28 '18 at 3:19













  • What happens when you access http://phpmyadmin.example.com?

    – Richard Smith
    Nov 28 '18 at 9:21











  • I get this error message: Hmm. We’re having trouble finding that site. We can’t connect to the server at phpmyadmin.example.com

    – Rahul
    Nov 28 '18 at 15:35

















your config seems right. but sites-enabled is not the place to configure domains, so sites-available is.

– rüff0
Nov 28 '18 at 3:19







your config seems right. but sites-enabled is not the place to configure domains, so sites-available is.

– rüff0
Nov 28 '18 at 3:19















What happens when you access http://phpmyadmin.example.com?

– Richard Smith
Nov 28 '18 at 9:21





What happens when you access http://phpmyadmin.example.com?

– Richard Smith
Nov 28 '18 at 9:21













I get this error message: Hmm. We’re having trouble finding that site. We can’t connect to the server at phpmyadmin.example.com

– Rahul
Nov 28 '18 at 15:35





I get this error message: Hmm. We’re having trouble finding that site. We can’t connect to the server at phpmyadmin.example.com

– Rahul
Nov 28 '18 at 15:35












1 Answer
1






active

oldest

votes


















1














I solved my problem and I am going to list out the steps I took to resolve the issue as future reference for others:



NGINX Configuration



The nginx configuration I tried initially was correct (as posted in the question).



Adding a CNAME record into DNS entries



This was the major missing piece. I added a CNAME entry into my DNS dashboard as follows:



Type: CNAME Name: phpmyadmin Value: example.com TTL: 3600 (can also set as automatic)



Expanding SSL Certificate



I use certbot for the SSL certificate, and I had to regenerate it to include the domain in the certificate.



sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com



During this step, certbot can take care of the nginx configuration to redirect HTTP traffic to HTTPS.



Reload NGINX service



Reloaded the new configuration: sudo service nginx reload



And verified the service status: sudo service nginx status



Everything is working well now.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511158%2fnginx-pointing-a-subdomain-to-a-different-directory-that-is-not-a-subdirectory%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I solved my problem and I am going to list out the steps I took to resolve the issue as future reference for others:



    NGINX Configuration



    The nginx configuration I tried initially was correct (as posted in the question).



    Adding a CNAME record into DNS entries



    This was the major missing piece. I added a CNAME entry into my DNS dashboard as follows:



    Type: CNAME Name: phpmyadmin Value: example.com TTL: 3600 (can also set as automatic)



    Expanding SSL Certificate



    I use certbot for the SSL certificate, and I had to regenerate it to include the domain in the certificate.



    sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com



    During this step, certbot can take care of the nginx configuration to redirect HTTP traffic to HTTPS.



    Reload NGINX service



    Reloaded the new configuration: sudo service nginx reload



    And verified the service status: sudo service nginx status



    Everything is working well now.






    share|improve this answer




























      1














      I solved my problem and I am going to list out the steps I took to resolve the issue as future reference for others:



      NGINX Configuration



      The nginx configuration I tried initially was correct (as posted in the question).



      Adding a CNAME record into DNS entries



      This was the major missing piece. I added a CNAME entry into my DNS dashboard as follows:



      Type: CNAME Name: phpmyadmin Value: example.com TTL: 3600 (can also set as automatic)



      Expanding SSL Certificate



      I use certbot for the SSL certificate, and I had to regenerate it to include the domain in the certificate.



      sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com



      During this step, certbot can take care of the nginx configuration to redirect HTTP traffic to HTTPS.



      Reload NGINX service



      Reloaded the new configuration: sudo service nginx reload



      And verified the service status: sudo service nginx status



      Everything is working well now.






      share|improve this answer


























        1












        1








        1







        I solved my problem and I am going to list out the steps I took to resolve the issue as future reference for others:



        NGINX Configuration



        The nginx configuration I tried initially was correct (as posted in the question).



        Adding a CNAME record into DNS entries



        This was the major missing piece. I added a CNAME entry into my DNS dashboard as follows:



        Type: CNAME Name: phpmyadmin Value: example.com TTL: 3600 (can also set as automatic)



        Expanding SSL Certificate



        I use certbot for the SSL certificate, and I had to regenerate it to include the domain in the certificate.



        sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com



        During this step, certbot can take care of the nginx configuration to redirect HTTP traffic to HTTPS.



        Reload NGINX service



        Reloaded the new configuration: sudo service nginx reload



        And verified the service status: sudo service nginx status



        Everything is working well now.






        share|improve this answer













        I solved my problem and I am going to list out the steps I took to resolve the issue as future reference for others:



        NGINX Configuration



        The nginx configuration I tried initially was correct (as posted in the question).



        Adding a CNAME record into DNS entries



        This was the major missing piece. I added a CNAME entry into my DNS dashboard as follows:



        Type: CNAME Name: phpmyadmin Value: example.com TTL: 3600 (can also set as automatic)



        Expanding SSL Certificate



        I use certbot for the SSL certificate, and I had to regenerate it to include the domain in the certificate.



        sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com



        During this step, certbot can take care of the nginx configuration to redirect HTTP traffic to HTTPS.



        Reload NGINX service



        Reloaded the new configuration: sudo service nginx reload



        And verified the service status: sudo service nginx status



        Everything is working well now.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 5:05









        RahulRahul

        1469




        1469
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511158%2fnginx-pointing-a-subdomain-to-a-different-directory-that-is-not-a-subdirectory%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)