Learn about Centmin Mod LEMP Stack today
Register Now

Nginx redirect to your website

Discussion in 'Install & Upgrades or Pre-Install Questions' started by dooma, Oct 19, 2016.

  1. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    Hello,

    I'm trying to redirect my website link Website Hosting - Mysite.com to www.mysite.com/forum

    I tried this
    Code:
    location /index.html { rewrite ^(.*)$ http://www.mysite.com/community/ redirect; }
    But I got that error :
    -bash: syntax error near unexpected token `('

    and when I tried this one :
    Code:
    location / {
      location = /index.php {
        return 302 http://www.domain.com/forum/;
      }
    }
    
    I got error that location command not found ..

    How can you help me regarding that ? ??

    Thanks
     
    Last edited: Oct 21, 2016
  2. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    where did you add ?

    When you create a new nginx vhost domain via centmin.sh menu option 2 or menu option 22 or via /usr/bin/nv cli command line, you will create the Nginx vhost files and directories. You will get an outputted the path location where it will create the domain name's vhost conf file named newdomain.com.conf (and newdomain.com.ssl.conf if you selected yes to self signed SSL)
    • Nginx vhost conf path will be at /usr/local/nginx/conf/conf.d/newdomain.com.conf
    • Nginx HTTP/2 SSL vhost conf path will be at /usr/local/nginx/conf/conf.d/newdomain.com.ssl.conf
    • Nginx Self-Signed SSL Certificate Directory at /usr/local/nginx/conf/ssl/newdomain.com
    • Vhost public web root will be at /home/nginx/domains/newdomain.com/public
    • Vhost log directory will be at /home/nginx/domains/newdomain.com/log
    Please post the contents of /usr/local/nginx/conf/conf.d/newdomain.com.conf and if applicable /usr/local/nginx/conf/conf.d/newdomain.com.ssl.conf wrapped in CODE tags (outlined at How to use forum BBCODE code tags)
     
  3. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    When I tried to post the contents of /usr/local/nginx/conf/conf.d/newdomain.com.conf , it gives me Permission Denied ?
     
  4. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    post to forums here ? how are you posting the contents ?

    just use cat to output contents of /usr/local/nginx/conf/conf.d/newdomain.com.conf in ssh client session
    Code (Text):
    cat /usr/local/nginx/conf/conf.d/newdomain.com.conf

    then copy and paste output to pastebin/gist.github.com or forum here in CODE tags
     
  5. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    That's the result:

    Code:
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    #server {
    #            listen   80;
    #            server_name mysite.com;
    #            return 301 $scheme://www.mysite.com$request_uri;
    #       }
    
    server {
      server_name mysite.com www.mysite.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/mysite.com/log/access.log combined buffer=                     256k flush=60m;
      error_log /home/nginx/domains/mysite.com/log/error.log;
    
      root /home/nginx/domains/mysite.com/public;
    
      # prevent access to ./directories and files
      location ~ (?:^|/)\. {
       deny all;
      }
    
      location / {
    
    # block common exploits, sql injections etc
    #include /usr/local/nginx/conf/block.conf;
    
      # Enables directory listings when index file not found
      #autoindex  on;
    
      # Shows file listing times as local time
      #autoindex_localtime on;
    
      # Enable for vBulletin usage WITHOUT vbSEO installed
      # More example Nginx vhost configurations at
      # http://centminmod.com/nginx_configure.html
      #try_files    $uri $uri/ /index.php;
    
      }
    
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    
     
  6. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    is there really that much space in access_log line between equal sign and 256k ?
    Code (Text):
    access_log /home/nginx/domains/mysite.com/log/access.log combined buffer=                     256k flush=60m;
    

    should be
    Code (Text):
    access_log /home/nginx/domains/mysite.com/log/access.log combined buffer=256k flush=60m;
    


    as to redirect did you edit /usr/local/nginx/conf/conf.d/newdomain.com.conf and replace

    Code (Text):
      location / {
    
    # block common exploits, sql injections etc
    #include /usr/local/nginx/conf/block.conf;
    
      # Enables directory listings when index file not found
      #autoindex  on;
    
      # Shows file listing times as local time
      #autoindex_localtime on;
    
      # Enable for vBulletin usage WITHOUT vbSEO installed
      # More example Nginx vhost configurations at
      # http://centminmod.com/nginx_configure.html
      #try_files    $uri $uri/ /index.php;
    
      }

    with
    Code (Text):
    location / {
      location = /index.php {
        return 302 http://www.domain.com/forum/;
      }
    }


    basically the location /{} context inner contents between outer most {} brackets needs changing
     
    Last edited: Oct 21, 2016
  7. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    Sorry, I didn't get what should I do ?

    thanks
     
  8. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
  9. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    No I didn't edit it. I just entered what I write at the thread .
     
  10. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    I don't know how can I change or replace something ?, I'm trying but all failed ?!
     
  11. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    need to learn how to edit using linux text editor in ssh
    read up on nano text editor - so you can edit files in sshd via nano linux text editor - read up more about nano here and here.

    practice on test text files in ssh session
     
  12. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    I have modified it using nano but nothing redirected. please check the result of the file now :

    Code:
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    #server {
    #            listen   80;
    #            server_name mysite.com;
    #            return 301 $scheme://www.mysite.com$request_uri;
    #       }
    
    server {
      server_name mysite.com www.mysite.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/mysite.com/log/access.log combined buffer=256k flush=60m;
      error_log /home/nginx/domains/mysite.com/log/error.log;
    
      root /home/nginx/domains/mysite.com/public;
    
      # prevent access to ./directories and files
      location ~ (?:^|/)\. {
       deny all;
      }
    
      location / {
     location = /index.php {
        return 302 http://www.mysite.com/community/;
    }
    
    }
    
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    
     
  13. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    whenever you edit nginx files, you need to restart nginx server for the changes to take effect either
    Code (Text):
    service nginx restart

    or command shortcut for centmin mod lemp stacks (faq item 16 has list of command shortcuts)
    Code (Text):
    ngxrestart

    then you can check your redirect headers using curl command in ssh and look for where the location header redirects to
    Code (Text):
    curl -I http://www.mysite.com/index.php

    you have the 302 temp redirect for www version of your site
    Code (Text):
     location = /index.php {
        return 302 http://www.mysite.com/community/;
    }
     
  14. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    I have restarted nginx and I checked curl and that was the result :

    Code:
    Restarting nginx (via systemctl):                          [  OK  ]
    [user@server01 ~]$ curl -I http://www.mysite.com/index.php
    HTTP/1.1 302 Moved Temporarily
    Date: Fri, 21 Oct 2016 14:12:50 GMT
    Content-Type: text/html
    Content-Length: 154
    Connection: keep-alive
    Location: http://www.mysite.com/community/
    Server: nginx centminmod
    
    when I go to www.mysite.com/index.php, it takes me to the community correctly. But I want the redirect to be correct when I go to www.mysite(dot)com

    Thanks
     
  15. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    do you have a index.html file in same page as your index.php file at your web root at /home/nginx/domains/mysite.com/public ?

    if you do the index.html by default takes priority over index.php if you visit web root of site unless you edit

    Code (Text):
      location / {
     location = /index.php {
        return 302 http://www.mysite.com/community/;
    }

    to below to add index and redefine the priority order in this case index.php first then index.html then index.htm
    Code (Text):
      location / {
     index index.php index.html index.htm;
     location = /index.php {
        return 302 http://www.mysite.com/community/;
    }
     
    Last edited: Oct 22, 2016
  16. dooma

    dooma Premium Member Premium Member

    220
    21
    18
    Oct 15, 2016
    Cairo
    Ratings:
    +26
    Local Time:
    3:38 AM
    when I go to mysite.com it gives me :
    403 Forbidden
    nginx
     
  17. eva2000

    eva2000 Administrator Staff Member

    28,926
    6,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,744
    Local Time:
    11:38 AM
    Nginx 1.13.x
    MariaDB 5.5
    did you upload your files to web root of your account at /home/nginx/domains/mysite.com/public ?

    what's output for this command in SSH client
    Code (Text):
    ls -lah /home/nginx/domains/mysite.com/public | egrep 'index.'


    you'd have your index.php page and/or index.html

    if it's empty without either page, then yes default nginx will give 403 error permission denied as it expects an index page. So you can just create a dummy index.php at /home/nginx/domains/mysite.com/public

    if index.php doesn't exist at /home/nginx/domains/mysite.com/public, using ssh client type
    Code (Text):
    cd /home/nginx/domains/mysite.com/public
    touch index.php
    

    to create an empty index.php file at /home/nginx/domains/mysite.com/public