This is the 8th day of my participation in Gwen Challenge

Nginx does reverse proxy

Install Nginx

# add Nginx source
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

# to install Nginx
sudo yum install -y nginx

# start Nginx
sudo systemctl start nginx.service

# set Nginx to boot
sudo systemctl enable nginx.service
Copy the code

Configure Nginx

The configuration file is usually named nginx.conf. The simplest configuration is as follows

server {
    listen 80;

    server_name example.com www.example.com;

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_passhttp://127.0.0.1:3000/; }}Copy the code
  • server_name:example.com www.example.comChange the domain name to its own.
  • proxy_pass:http://127.0.0.1:3000/Change the value to a deployed service
Check whether the configuration is incorrect
sudo nginx -t

# Reload the Nginx configuration
sudo nginx -s reload
Copy the code

Configuring an SSL Certificate

The appearance of this operation is to replace HTTP with HTTPS. For details, see computer networking

Note: Port 443 needs to be enabled on the firewall

The code below is for automatically applying for a free SSL certificate. If you have prepared your own SSL certificate, check out the tutorial provided with the certificate.

Install certbot and the Certbot nginx plugin
sudo yum install certbot python2-certbot-nginx -y

# During the configuration, your email will be asked, just fill it in
sudo certbot --nginx

# Automatic renewal
sudo certbot renew --dry-run
Copy the code

At this point, the configuration of Nginx is complete, you can visit your own domain to see the effect.

Caddy does the reverse proxy

Caddy is a Web server developed using the Go language. Its configuration is more concise, and can automatically apply for and configure SSL certificates, these two years is very hot an HTTP Server

Some people say Caddy is not as good as Nginx in performance, I don’t know, but it’s definitely more user-friendly in overhand/configuration difficulty.

Caddy supports automatic signing of Let’s Encrypt SSL certificates by providing a mailbox and applying for, configuring, and renewing SSL certificates himself. (No more overconfiguring)

Install the Caddy fixings

Install the Caddy package
yum install caddy -y
Copy the code

Configuration Caddy fixings

Use vim to edit Caddyfile
vim /etc/caddy/conf.d/Caddyfile.conf
Copy the code
https://www.simple.com {
    gzip
        tls [email protected]
        proxy / localhost:port {
        transparent}}Copy the code
  1. thehttps://www.simple.comChange to your own domain name.
  2. tlsAt the back of the[email protected]Change to your own email address, which is used to automatically apply for SSL certificates. Note that you do not need to configure the SSL certificate yourself, and will automatically renew it for you.
  3. localhost:portPlease sendportChange the value to the running port of the service

Start the Caddy service after the modification is complete.

# Enable Caddy service
systemctl enable caddy

# start Caddy fixings
service caddy start

Stop running Caddy
service caddy stop

# restart Caddy fixings
service caddy restart

Check Caddy status
service caddy status
Copy the code

If the Caddy fixings start appear such as [/ usr/lib/systemd/system/Caddy fixings. Service: 23] Unknown lvalue ‘AmbientCapabilities’ in section’ service ‘and so on

Please use yum update-y to update the system.

Then run the service caddy restart command to restart CentOS 7.3.

Configure multiple urls to redirect to the main url, such as simple.com to www.simple.com

Use vim to edit Caddyfile
vim /etc/caddy/conf.d/Caddyfile.conf
Copy the code

We add the following configuration to the original

https://simple.com {
    redir https://www.simple.com{url}
}
Copy the code

Just change https://simple.com and https://www.simple.com{url} to the urls you want, for example if I want to go to xn2001.com and jump to www.xn2001.com

https://xn2001.com {
    redir https://www.xn2001.com{url}
}

https://www.xn2001.com {
    gzip
        tls [email protected]
        proxy / localhost:3000 {
        transparent}}Copy the code

Finally, let’s restart Caddy.

Now that the Caddy reverse proxy configuration is complete, you can visit your own domain name to test the effect.