Proxy deployment involves placing a layer of third-party WebServer
server in front to handle all requests, selectively forwarding some requests (often dynamically processed requests) to the backend Golang
applications for execution. The backend-deployed Golang
applications can be configured with multiple instances. This approach is commonly used in complex WebServer
configurations, such as scenarios requiring static file separation, multiple domain and certificate configurations, custom load balancing layers, and more.
Although Golang
implemented WebServer
can handle static files, it is relatively simple and less performant compared to professional WebServer
like nginx
/ apache
. Therefore, using Golang WebServer
as the front-end service to directly handle static file requests is not recommended.
Nginx
We recommend using Nginx
as a reverse proxy front-end access layer, which has two configuration methods for separating static and dynamic requests.
Static File Suffix
This approach differentiates by file name suffix, delegating specified static files to nginx
for processing, while other requests are forwarded to the golang
application. The configuration example is as follows:
server {
listen 80;
server_name goframe.org;
access_log /var/log/gf-app-access.log;
error_log /var/log/gf-app-error.log;
location ~ .*\.(gif|jpg|jpeg|png|js|css|eot|ttf|woff|svg|otf)$ {
access_log off;
expires 1d;
root /var/www/gf-app/public;
try_files $uri @backend;
}
location / {
try_files $uri @backend;
}
location @backend {
proxy_pass http://127.0.0.1:8199;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Here, 8199
is the local golang
application WebServer
listening port.
For example, under this configuration, you can access the specified static file via http://goframe.org/my.png
.
Static File Directory
This method distinguishes by file directory, delegating access requests for specified directories to nginx
, while other requests are forwarded to the golang
application. The configuration example is as follows:
server {
listen 80;
server_name goframe.org;
access_log /var/log/gf-app-access.log;
error_log /var/log/gf-app-error.log;
location ^~ /public {
access_log off;
expires 1d;
root /var/www/gf-app;
try_files $uri @backend;
}
location / {
try_files $uri @backend;
}
location @backend {
proxy_pass http://127.0.0.1:8199;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Here, 8199
is the local golang
application WebServer
listening port.
For example, under this configuration, you can access static files via http://goframe.org/public/my.png
.