Simple Configuration of Nginx Reverse Proxy

Publish: 2015-11-24 | Modify: 2015-11-24

Blogs have always been hosted in the DigitalOcean San Francisco data center. Recently, some friends may have noticed that the telecommunications line to DigitalOcean San Francisco has been experiencing severe packet loss, resulting in a terrible browsing experience. To ensure the normal access of the website, what can be done with such slow speed? I thought of using Nginx reverse proxy.

What is a reverse proxy? Here is Baidu's explanation. Reverse proxy is a method in which a proxy server accepts connection requests from the Internet, forwards the requests to servers on the internal network, and returns the results obtained from the servers to the clients that requested the connection on the Internet. At this time, the proxy server appears to the outside world as a reverse proxy server.

Actually, I'm not very familiar with Nginx configuration, but referring to related documents online, it's not difficult to implement basic reverse proxy. Let me share it with you below. This method is suitable for Junge's LNMP one-click package and AMH hosting panel (of course, AMH comes with a reverse proxy module). Let's get started.

Here is a very simple reverse proxy configuration:

server
{
    listen          80;
    server_name     xiaoz.me www.xiaoz.me;
    location / {
        proxy_pass http://www.baidu.com;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Explanation of the fields:

  • server_name: Your own domain name.
  • proxy_pass: The domain name to be proxied. In this case, it is set to Baidu's address. So, when you visit www.xiaoz.me, it will request Baidu's content and return it to the client.

Save the above code as "YourDomainName.conf", such as "xiaoz.me.conf", and then place this configuration file in the "/usr/local/nginx/conf/vhost" directory on the server. Finally, restart the Nginx service. For LNMP 1.2, enter the command "lnmp nginx restart". For AMH 4.2, enter the command "amh nginx restart" to take effect.

Finally, resolve your domain name to the server for access. The above rules only implement the most basic reverse proxy. Additionally, you can use Nginx rules to achieve keyword replacement, caching directories, etc. If you are interested, you can search for more related information about Nginx online.


Comments