Home > Article > Backend Development > How to deploy golang in nginx
In recent years, Golang language has been favored by developers for its high concurrency performance, concise syntax and rapid development speed. However, in an actual production environment, Golang applications also need an efficient server to support their operation. This article will introduce how to use Nginx to deploy Golang applications.
1. Introduction to Nginx
Nginx is a lightweight, high-performance web server software. It is one of the most popular web server software currently and is very suitable for load balancing and reverse proxy. and other high-performance services. Compared with Apache, Nginx has a smaller memory footprint, stronger anti-concurrency capabilities and higher performance. Therefore, Nginx is increasingly used in high-load web service clusters.
2. Overview of Golang deployment scheme
Generally speaking, the deployment of Golang applications can be divided into the following aspects:
Next, we will introduce in detail how to use Nginx to implement reverse proxy and load balancing, Makes Golang applications more efficient and stable during deployment.
3. Install Nginx and related components
1. Install Nginx
Simply execute the following command to complete the simple installation of Nginx:
$ sudo apt-get update $ sudo apt-get install nginx
Installation After completion, use the following command to start Nginx:
$ sudo systemctl start nginx
2. Create a Golang application
Here to demonstrate how to deploy a Golang application, we have written a simple hello.go file with the following code:
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, Nginx!") }) http.ListenAndServe(":8080", nil) }
3. Compile the binary file
In the root directory of the Golang application, execute the following command to compile and generate the binary file:
$ go build -o myapp
The generated binary file will be placed in in the myapp file in the current directory.
4. Run the Golang application
Execute the following command in the command line terminal to run the Golang application:
$ ./myapp
After running, you can use localhost:8080 in the browser Visit the app.
4. Configure Nginx
1. Reverse proxy
In order to facilitate the deployment of Golang applications, we need to use Nginx reverse proxy requests. Edit the Nginx configuration file /etc/nginx/nginx.conf and modify it to the following content:
user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; multi_accept on; } http { upstream myapp{ server 127.0.0.1:8080; } server { listen 80; server_name localhost; location / { proxy_pass http://myapp; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } }
After the modification is completed, use the following command to restart Nginx to make the configuration take effect:
$ sudo systemctl restart nginx
Next, Go to localhost in the browser address bar to access the Golang application.
2. Load balancing
For high-concurrency scenarios in production environments, we need to use load balancing mechanisms to improve system performance and stability. Nginx provides a variety of load balancing strategies, including polling, IP Hash, Weighted Endpoint, etc. In this article, we take the polling strategy as an example to introduce how to configure Nginx load balancing.
In the Nginx configuration file, modify the configuration of the upstream part to achieve load balancing:
upstream myapp { server 127.0.0.1:8080; server 127.0.0.1:8090; server 127.0.0.1:8100; } server { listen 80; server_name localhost; location / { proxy_pass http://myapp; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Here we have configured three Golang application instances, and Nginx will poll the request distributed to each instance. After the modification is completed, restart Nginx.
5. Use Supervisor for process management
In a production environment, we need to use process management tools to manage applications. Supervisor is a commonly used process management tool.
First, install Supervisor:
$ sudo apt-get install supervisor
Then, create a Supervisor configuration file, such as myapp.conf, the content of the configuration file is as follows:
[program:myapp] command=/path/to/myapp autostart=true autorestart=true user=username
Among them, command is the command to be executed , autostart and autorestart are Supervisor automatic startup and restart applications. Use the following command to start Supervisor:
$ sudo systemctl start supervisor
Supervisor will regularly check whether the application is running. If it finds that the program exits abnormally, it will automatically restart the application.
6. Summary
This article introduces how to use Nginx to deploy Golang applications and use Supervisor for process management. Through reverse proxy and load balancing mechanisms, we can easily deploy high-performance and stable Golang applications. It is recommended that readers make appropriate adjustments and optimizations in actual production environments.
The above is the detailed content of How to deploy golang in nginx. For more information, please follow other related articles on the PHP Chinese website!