DEV Community

Cover image for Setting Up Nginx as a Reverse Proxy
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Setting Up Nginx as a Reverse Proxy

This article was originally published on bmf-tech.com.

I configured Nginx as a reverse proxy on a Sakura VPS that was previously running Apache, so I'm jotting down some notes.

It's been a while since I set it up, so I might not remember everything perfectly. Please bear with me.

Environment

  • Sakura VPS
  • CentOS 6 series
  • Apache 2.2.15
  • Nginx 1.8.1

Recommended Knowledge

  • Understanding and configuring Apache virtual hosts

In simple terms, Nginx receives requests and forwards them to a specified port on Apache. The virtual host settings are configured on the Apache side. Nginx just acts as a pass-through.

Installing Nginx

Using wget and yum to download and install was straightforward. I won't cover the installation here, so please install it according to your environment.

Once installed, stop Apache and verify Nginx's operation.

Changing Apache's Port

We'll use port 80 for Nginx. Specify a different port for Apache. Here, we'll use port 8080.

/etc/httpd/conf/httpd.conf

NameVirtualHost *:8080

<VirtualHost *:8080>

hogehogehogehoge...

</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Note
Check iptables with:
iptables -L

The location of iptables is:
/etc/sysconfig/iptables

If you have virtual host settings for a custom domain, change those ports as well.

Ex. /etc/httpd/conf.d/hoge.com.conf

# Domain
<VirtualHost *:8080>
  ServerName hoge.com
  DocumentRoot "/var/www/html/hoge"
  DirectoryIndex index.html index.php
  ErrorLog /var/log/httpd/error_log
  CustomLog /var/log/httpd/access_log combined
  AddDefaultCharset UTF-8
  <Directory "/var/www/html/hoge">
    AllowOverride All
  </Directory>
</VirtualHost>

# Sub Domain
<VirtualHost *:8080>
  ServerName sub-hoge.hoge.com
  DocumentRoot "/var/www/html/sub-hoge"
  DirectoryIndex index.html index.php
  ErrorLog /var/log/httpd/error_log
  CustomLog /var/log/httpd/access_log combined
  AddDefaultCharset UTF-8
  <Directory "/var/www/html/sub-hoge">
    AllowOverride All
  </Directory>
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Configuring Reverse Proxy in Nginx

I referred to Coexisting Apache and Nginx and Gradually Migrating.

/etc/nginx/conf.d/reverse_proxy.conf

"reverse_proxy.conf" 14L, 392C
server {
        listen 80;

        location / {
                proxy_pass http://127.0.0.1:8080;
                proxy_redirect                         off;
                proxy_set_header Host                  $host;
                proxy_set_header X-Real-IP             $remote_addr;
                proxy_set_header X-Forwarded-Host      $host;
                proxy_set_header X-Forwarded-Server    $host;
                proxy_set_header X-Forwarded-For       $proxy_add_x_forwarded_for;
        }
}

Enter fullscreen mode Exit fullscreen mode

Finally, restart Apache and Nginx to complete the setup!

Thoughts

The performance improvement is noticeable... somewhat faster, perhaps?

I have much to learn about infrastructure setup, so I'll strive to improve in the future.

Top comments (0)