Want to subscribe to topics you're interested in?
Become a Member

Nginx How To Change Default Nginx Vhost

Discussion in 'Nginx and PHP-FPM news & discussions' started by hendry, Dec 26, 2016.

  1. hendry

    hendry Member

    59
    14
    8
    Sep 19, 2016
    Ratings:
    +18
    Local Time:
    11:59 AM
    nginx/1.11.4
    MariaDB 10
    When I create bulk vhost or create vhost WP with option 22 the "try_files" always show with hashtag and I want to remove that hashtag when I create vhost for my domain. I have try to uncomment the hashtag on this "/usr/local/nginx/conf/conf.d/virtual.conf" but the hashtag in try_files still appear when I create vhost.


    Because I always use Wordpress I want my default vhost look like this when I create it.
    Code (Text):
    try_files               $uri $uri/ /index.php?q=$request_uri;
    


    Anyone can help me?
     
  2. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    2:59 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    the default main hostname vhost at /usr/local/nginx/conf/conf.d/virtual.conf is not made for web sites as it's expected to be used by Centmin Mod stats pages etc i.e. PHP Opcode and Memcached statistics pages

    Getting Started Guide explains what main hostname and nginx added vhosts are for Getting Started Guide - CentminMod.com LEMP Nginx web stack for CentOS

    You should not use main hostname and vhost at /usr/local/nginx/conf/conf.d/virtual.conf for hosting web sites

    If you mean default template vhost for centmin.sh menu option 2 added vhosts, then yes you have to edit them each time manually. If you use centmin.sh menu option 22 wordpress installer then vhost is automatically configured for wordpress :)
     
  3. hendry

    hendry Member

    59
    14
    8
    Sep 19, 2016
    Ratings:
    +18
    Local Time:
    11:59 AM
    nginx/1.11.4
    MariaDB 10
    Yes, this is I mean but i will create it with this command.
    Code (Text):
    while read -r d s f; do echo "nv -d $d -s $s -u $f"; nv -d $d -s $s -u $f; done < /home/vhostlist.txt


    OMG @eva2000 my further works is build a thousand domains and you can imagine if I do this manually for a thousand domains that i'll build it at once my hands will need a lot of ice :LOL::LOL::LOL:

    how if I add try_files in this file "/usr/local/nginx/conf/nginx.conf", can it work? or any solution for this?
     
  4. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    2:59 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Yeah not right now. @ethanpil has a suggestion at Additions to custom_config.inc for persistent NGINX .conf but that still won't help you for editing the existing template. Will need to think about how to best do this for the future as definitely a solution is needed.
     
  5. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    2:59 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    @hendry you could use sed to do a replacement for
    from
    Code (Text):
    #try_files    $uri $uri/ /index.php;
    

    to
    Code (Text):
    try_files               $uri $uri/ /index.php?q=$request_uri;
    

    single vhost sed replacement would be where ${d} is your domain.com name
    Code (Text):
    sed -i 's|#try_files    $uri $uri/ /index.php;|try_files   $uri $uri/ /index.php?q=$request_uri;|' /usr/local/nginx/conf/conf.d/${d}.conf
    

    so for mass nginx vhost generation via nv command line
    Code (Text):
    while read -r d s f; do echo "nv -d $d -s $s -u $f"; nv -d $d -s $s -u $f; sed -i 's|#try_files    $uri $uri/ /index.php;|try_files   $uri $uri/ /index.php?q=$request_uri;|' /usr/local/nginx/conf/conf.d/${d}.conf; done < /home/vhostlist.txt
    
     
  6. hendry

    hendry Member

    59
    14
    8
    Sep 19, 2016
    Ratings:
    +18
    Local Time:
    11:59 AM
    nginx/1.11.4
    MariaDB 10
    that command doesn't work.. hashtag still persist on domain vhost
     
  7. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    2:59 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Any errors ? did you have self-signed ssl set to yes = -s y in vhoslist.txt probably why as command is for non-https vhosts
     
  8. hendry

    hendry Member

    59
    14
    8
    Sep 19, 2016
    Ratings:
    +18
    Local Time:
    11:59 AM
    nginx/1.11.4
    MariaDB 10
    No error with that command and all domain vhost I set without ssl.
     
  9. hendry

    hendry Member

    59
    14
    8
    Sep 19, 2016
    Ratings:
    +18
    Local Time:
    11:59 AM
    nginx/1.11.4
    MariaDB 10
    @eva2000 problem solved. I thought sed command didn't work if we use that command with long try_files string, so I try to separate the string that I want to change, in this case is uncommand hashtag on try_files and adding index.php?q=$request_uri;

    So I edit your previous command with this:
    Code (Text):
    while read -r d s f; do echo "nv -d $d -s $s -u $f"; nv -d $d -s $s -u $f; sed -i 's|#try_files|try_files|g' /usr/local/nginx/conf/conf.d/${d}.conf; sed -i 's|index.php;|index.php?q=$request_uri;|g' /usr/local/nginx/conf/conf.d/${d}.conf; done < /home/vhostlist.txt


    I hope this is useful for other members. Cheers ;)
     
  10. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    2:59 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    ah excellent work :)