ubuntu system apache configuration virtual host and reverse proxy details

  • 2020-06-07 05:58:43
  • OfStack

preface

I have two domain names blogsir. com. cn and s7star. cn, both of which are bound to my Tencent Cloud host.

Configure the virtual host


$ sudo vi /etc/apache2/sites-available/000-default.conf
#DocumentRoot /var/www/html  ## Comment out the line 
$ cat /etc/apache2/sites-available/000-default.conf|grep -v "#"
<VirtualHost *:80>
ServerAdmin webmaster@localhost
   ServerName www.blogsir.com.cn
   DocumentRoot "/var/www/html/"
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
   ServerName www.s7star.cn
   DocumentRoot "/var/www/xx" 
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note 1: * here means 127.0.01

Modify the host file and add the following


$ sudo vi /etc/hosts
127.0.0.1 www.blogsir.com.cn
127.0.0.1 www.s7star.cn

Configure the port reverse proxy

If the port is not 80, you can configure the reverse proxy, such as my ctf s7star. cn, start is through port 4000 to access, but not very convenient, after the reverse proxy configuration convenient a lot

The reverse proxy wiki explains that it retrieves resources from the server on the back end at the client's request, and then returns those resources to the client. Unlike the forward proxy, which ACTS as a medium to return resources acquired over the Internet to the associated client, the reverse proxy is used as a proxy on the server side, not on the client side.

The port of the blog is 4000. After using the reverse proxy, the user will return to the site of port 80 and apache will display the content of the blog to the user, just like the user directly accesses port 4000 1.

Here are the steps to configure apache:

1. Load the apache module and use the command a2enmod to load the module


a2enmod proxy proxy_balancer proxy_http

The command needs to be used when the load is complete /etc/init.d/apache2 restart Restart the server

2. Configure the reverse proxy function, enter sites_available, edit the file 000-ES48en.conf, and add the following code


<VirtualHost *:80>
  ServerName ctf.s7star.cn
  DocumentRoot "/var/www/xx/"
  ErrorLog ${APACHE_LOG_DIR}/ctf_errors.log
  CustomLog ${APACHE_LOG_DIR}/ctf_accesses.log combined
  ProxyPass / http://127.0.0.1:4000/
  ProxyPassReverse / http://127.0.0.1:4000/
</VirtualHost>

Finally, restart apache, and when you open the page again, it will redirect you to the blog at port 4000

conclusion


Related articles: