Project

General

Profile

Support #856

Updated by Daniel Curtis about 8 years ago

{{>toc}} 

 One of the websites I am working on at the moment isn't quite ready for production, but the domain name has already been purchased and I want *something* to show up when visited. To accomplish this I've setup a separate folder with Oleose, an open source landing page template, and set nginx to redirect to the landing page if not connected from localhost.  

 This is a guide on how to setup a landing page and redirect incoming Internet connections to the landing page with nginx on FreeBSD 10. 

 *NOTE*: This setup is done without performance-costing "if" statements; but rather sets the landing page as a custom access denied page, then using nginx's built-in access control list to deny all connections to the main website except for connections from 127.0.0.1. 

 h2. Install Nginx 

 * Install nginx: 
 <pre> 
 pkg install nginx 
 </pre> 

 * Start and enable nginx at boot: 
 <pre> 
 echo 'nginx_enable="YES"' >> /etc/rc.conf 
 service nginx start 
 </pre> 

 * Create a configuration directory to make managing individual server blocks easier 
 <pre> 
 mkdir /usr/local/etc/nginx/conf.d 
 </pre> 

 * Edit the main nginx config file: 
 <pre> 
 vi /usr/local/etc/nginx/nginx.conf 
 </pre> 
 #* And strip down the config file and add the include statement at the end to make it easier to handle various server blocks: 
 <pre> 
 worker_processes    1; 
 error_log    /var/log/nginx-error.log; 

 events { 
     worker_connections    1024; 
 } 

 http { 
     include         mime.types; 
     default_type    application/octet-stream; 
     sendfile          on; 
     keepalive_timeout    65; 

     # Load config files from the /etc/nginx/conf.d directory 
     include /usr/local/etc/nginx/conf.d/*.conf; 
 } 
 </pre> 

 h3. Basic Website 

 * Add a *newsite.example.com server block*: 
 <pre> 
 vi /usr/local/etc/nginx/conf.d/newsite.example.com.conf 
 </pre> 
 #* Add the following: 
 <pre> 
 server { 
     listen         80; 
     server_name    newsite.example.com; 
     root           /usr/local/www/newsite.example.com; 
     access_log     /var/log/newsite.example.com-access.log; 
     error_log      /var/log/newsite.example.com-error.log; 

     location / { 
       try_files $uri $uri/ /index.html; 
     } 
 } 
 </pre> 

 * Restart nginx: 
 <pre> 
 service nginx restart 
 </pre> 

 h3. Oleose Landing Page 

 * Clone the oleose landing page from github: 
 <pre> 
 cd /usr/local/www 
 git clone https://github.com/ScoopThemes/Oleose.git landing-page 
 </pre> 

 * Edit the *newsite.example.com server block*: 
 <pre> 
 vi /usr/local/etc/nginx/conf.d/newsite.example.com.conf 
 </pre> 
 #* Add the following: 
 <pre> 
 server { 
     listen         80; 
     server_name    newsite.example.com; 
     root           /usr/local/www/newsite.example.com; 
     access_log     /var/log/newsite.example.com-access.log; 
     error_log      /var/log/newsite.example.com-error.log; 

     location ~ ^/(?:CHANGELOG\.md|README.md|.git|.gitignore){ 
       deny all; 
     } 

     location / { 
       error_page 403 = @internetland; 
       allow 127.0.0.1; 
       deny all; 
       try_files $uri $uri/ /index.html; 
     } 

     location @internetland { 
       return 301 $scheme://newsite.example.com/landing-page; 
     } 

     location /assets { 
       alias /usr/local/www/landing-page/assets; 
       index index.html 
       allow all; 
       try_files $uri $uri/; 
     } 

     location /landing-page { 
       alias /usr/local/www/landing-page; 
       index.html 
       allow all; 
       try_files $uri $uri/ /index.html; 
     } 
 } 
 </pre> 

 * Restart nginx: 
 <pre> 
 service nginx restart 
 </pre> 

 * Now open a web browser and go to http://newsite.example.com , you will find that all requests from the internet are sent to http://newsite.example.com/landing-page 

 *NOTE*: Since the normal website is blocked to all but connections from localhost, I use an SSH tunnel to log into the web server: 
 <pre> 
 ssh -L 8080:localhost:80 newsite.example.com 
 </pre> 
 Then open a web browser and go to http://localhost:8080 to tunnel all HTTP traffic on port 8080 on the local machine. 

 h2. Resources 

 * http://stackoverflow.com/questions/17993011/redirecting-all-requests-that-arent-from-my-ip-with-nginx

Back