The following are detailed steps and sample code for Nginx to configure reverse proxy to Gin (Go-based Web framework) application. Assuming that the Gin application runs on the local 127.0.0.1:8080 port (you can replace it according to the address and port of the actual Gin application running).
1. Install and start the Gin application
First, make sure you have developed the Gin application using Go and can be started and run locally. Here is a simple Gin application sample code (save as a file) to return a simple "Hello, World!" message:
package main import ( "net/http" "/gin-gonic/gin" ) func main() { r := () ("/", func(c *) { (, { "message": "Hello, World!", }) }) (":8080") }
Enter the directory where the code is located in the command line, execute the go run command to start the Gin application, and it will listen to port 127.0.0.1:8080 waiting for requests.
2. Install and configure Nginx
Install Nginx:
The way Nginx is installed varies in different operating systems.
On Ubuntu or Debian systems: You can install it using the following command:
sudo apt-get update sudo apt-get install nginx
In CentOS or RHEL systems: it can be installed with the following command:
sudo yum install epel-release sudo yum install nginx
In Windows systems: you can download the corresponding Windows version compression package from the official Nginx website (/), and it can be used after decompression. However, in production environments, Nginx is usually deployed using Linux.
Configure the reverse proxy of Nginx: The configuration file of Nginx is usually located in the /etc/nginx/ directory (different systems may vary slightly). The main configuration file is generally , but in order to facilitate management and modular configuration, we often create separate .conf files in the / directory to configure specific sites or reverse proxy and other contents.
Create a file named gin_proxy.conf (the file name can be set by yourself), and add the following configuration content:
server { listen 80; server_name your_domain.com; # Here is a replacement with the domain name you want to bind to. If you are only testing locally, you can write localhost location / { proxy_pass http://127.0.0.1:8080; # Point to the address and port where the Gin application runs proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
In the above configuration:
- The listen directive specifies the port for Nginx listening, which is set to port 80 (the default HTTP port). If you need to use HTTPS, you will also need to configure certificates and other related contents in the future and modify the listening port to 443.
- server_name is used to specify the server domain name. It can be written as localhost or 127.0.0.1 during local testing, and the real domain name is filled in in the actual production environment.
- location / block means matching all request paths (all requests under the root path). The proxy_pass directive is the key reverse proxy setting, forwarding the request to the 127.0.0.1:8080 port where the Gin application is located. The following several proxy_set_header instructions set request header related information, which helps Gin applications obtain the correct client related information, such as the original requested Host, the client's real IP address, etc.
3. Verify the configuration and restart Nginx
Verify the correctness of configuration syntax:
Execute the following command from the command line to check whether the Nginx configuration file has syntax errors (taking Linux system as an example):
sudo nginx -t
If the configuration syntax is correct, a message like the following will be displayed:
nginx: the configuration file /etc/nginx/ syntax is correct
nginx: configuration file /etc/nginx/ test is successful
Restart Nginx to make the configuration take effect:
Depending on the different operating systems, use the corresponding command to restart Nginx.
On Ubuntu or Debian systems:
sudo service nginx restart
In CentOS or RHEL systems:
sudo systemctl restart nginx
After completing the above steps, when you access the domain name or IP address that Nginx listens to through a browser or other client (for example, accessing http://localhost locally), Nginx will reverse proxy the request to the Gin application. After the Gin application processes the request, the corresponding response content will be returned, and finally displayed on the client.
Please note that if it is deployed in a server environment and involves providing services to the outside world, you may also need to consider the firewall opening corresponding ports, domain name resolution and other related settings to ensure that the entire process can work normally.
This is the article about the methods and steps of nginx configuration reverse proxy to gin. For more related content on nginx configuration reverse proxy to gin, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!