Set Domain-Based Server Name0:44
request to app1. And if the request is targeting domain2.com, you tell Nginx to serve app2. And this is what we are going to look into in this lesson. So let's get started. The first thing we are going to do is edit the Nginx configuration file for our laracasts app. So sudo nano /etc/nginx/sites-available/laracasts. What we want to do here is configure this Nginx server to serve this app only when the requests are targeting a specific domain. And to do that, we will edit the server_name directive, and replace the underscore with
requests are targeting a specific domain. And to do that, we will edit the server name directive, and replace the underscore with our domain name. I'll use this domain for the sake of the tutorial, go with php.com. Next, we want to stop making this server the default on port 80. So we will remove the default server flag from the ipv4 and ipv6 listeners. So we remove this, and this. Now let's save the file, and go create a default server block, one that will catch all requests coming to the ib address of the VBS. So sudo nano default.
Create Default Catch-All Server2:02
coming to the ib address of the VBS. So sudo nano default. Then inside the file, let's paste this server block. And here we are stating that this is the default server for requests coming on port 80 to this VBS. For those requests, we are going to serve the default Nginx index.nginx-debian.html file that is located inside the /var/www/html directory. You can replace this with a regular 404.html page for example so clients sending requests on the ib address know that this is not acceptable. Now let's save this file, and create a symlink inside the sites-enabled directory.
Enable Site and Add DNS2:44
on the ib address know that this is not acceptable. Now let's save this file, and create a symlink inside the sites-enabled directory. So sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/. And then finally we reload Nginx. sudo service nginx reload. Now let's visit the ib address in the browser. And here we go, our Nginx index file is served instead of the Laravel application. Let's then go to our DNS providers dashboard and add a record for the domain. We will add an A record for the name gowithphp.com and provide the ipv4 address of the server. Save the record.
Plan WWW Redirect for SEO4:20
So we want to be able to serve our website on the www subdomain in addition to the root domain. However, serving the same content on two different URLs affects our score in most search engines. And to avoid that, we want to redirect requests targeting the www subdomain to the root domain or the naked domain as many call it. We can of course perform this redirection from within our Laravel application. But why ask php fpm to handle a request that's just going to be redirected? php fpm loads our Laravel application in memory and does a lot of expensive work just to handle a request. We better allocate these resources only for requests that require handling by our Laravel
Add Nginx WWW Redirect Block5:02
a request. We better allocate these resources only for requests that require handling by our Laravel application. So luckily, Nginx can help us redirect those requests without ever hitting the Laravel application. Let's see how. We will edit our Nginx file that belongs to the laracasts app. So sudo nano /etc/nginx/sites-available/laracasts. Then at the top of the file, we will add another server block. This block will listen on port 80 for IPv4 and also for IPv6.
Then at the top of the file, we will add another server block. This block will listen on port 80 for ipv4 and also for ipv6. Then it will match requests targeting the www.gowithphp.com domain. So server name www.gowithphp.com. Then we tell Nginx to redirect these requests with a 301 status code to the naked domain. So return 301 http://gowithphp.com. Now let's save the file and reload Nginx. sudo service nginx reload. Let's then go to our DNS providers dashboard and add a record for the domain. We will add another A record for the name www and provide the ipv4 address of the server.
