Server
objects support convenient configuration through configuration files. Please refer to the API documentation for supported configuration options and explanations; the documentation provides detailed information, and the following sections will not introduce configuration options.
When using g.Server(singleton name)
to obtain a Server
singleton object, it will automatically obtain the corresponding Server
configuration through the default configuration management object. By default, it reads the server.singleton name
configuration item, and if that does not exist, it will read the server
configuration item.
For supported configuration file options, refer to the Server
configuration management object properties: https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#ServerConfig
Example 1, Default Configuration Items
server:
address: ":80"
serverRoot: "/var/www/Server"
Later, you can use g.Server()
to automatically obtain and set this configuration when acquiring the default singleton object.
If address
is not configured, the http server
will start using all ip
addresses of the local network card plus a random free port (default configuration :0
). If you want to specify an ip
but start the http server
with a random free port, you can configure address
in the format of ip:0
, for example: 192.168.1.1:0, 10.0.1.1:0
.
Example 2, Multiple Configuration Items
Example for multiple Server
configurations:
server:
address: ":80"
serverRoot: "/var/www/Server"
server1:
address: ":8080"
serverRoot: "/var/www/Server1"
server2:
address: ":8088"
serverRoot: "/var/www/Server2"
We can obtain the corresponding Server
singleton object through the singleton object name:
// Corresponding to server.server1 configuration item
s1 := g.Server("server1")
// Corresponding to server.server2 configuration item
s2 := g.Server("server2")
// Corresponding to default configuration item server
s3 := g.Server("none")
// Corresponding to default configuration item server
s4 := g.Server()
Example 3, More Complete Example
For example, for the example in the previous section, the corresponding configuration file is as follows:
server:
address: ":8199"
serverRoot: "/var/www/Server"
indexFiles: ["index.html", "main.html"]
accessLogEnabled: true
errorLogEnabled: true
pprofEnabled: true
logPath: "/var/log/ServerLog"
sessionIdName: "MySessionId"
sessionPath: "/tmp/MySessionStoragePath"
sessionMaxAge: "24h"
dumpRouterMap: false
Similarly, the names of configuration property items are not case-sensitive, and it also supports using -
/_
symbols to connect words. That means the following configuration file has the same effect as the above one:
server:
address: ":8199"
serverRoot: "/var/www/Server"
indexFiles: ["index.html", "main.html"]
accessLogEnabled: true
errorLogEnabled: true
pprofEnabled: true
log-path: "/var/log/ServerLog"
session_Id_Name: "MySessionId"
Session-path: "/tmp/MySessionStoragePath"
session_MaxAge: "24h"
DumpRouterMap: false
We recommend using camelCase format for configuration item names in configuration files.
Upload Limit
Server
has size limits for data submitted by clients, controlled by two main configuration parameters:
MaxHeaderBytes
: Request header size limit, the request header includesCookie
data submitted by the client, default is10KB
.ClientMaxBodySize
: Body size limit submitted by the client, also affecting file upload size, default is8MB
.
Due to security considerations, the default upload limits are not very high, especially the size limit of ClientMaxBodySize
. In scenarios where file uploads are needed, you may consider adjusting, and it can be configured through a configuration file, for example:
server:
maxHeaderBytes: "20KB"
clientMaxBodySize: "200MB"
This way, the request header size limit is modified to 20KB
, and the file upload size limit is 200MB
. If you do not want to impose any limit on the upload size, set clientMaxBodySize
to 0
.
Log Configuration
Starting from version v2
, Server
added support for Logger
configuration item in configuration files, mainly to unify log component configuration and solve log rolling split issues. Configuration example:
server:
address: ":8080"
logger:
path: "/var/log/server"
file: "{Y-m-d}.log"
stdout: false
rotateSize: "100M"
rotateBackupLimit: 10
rotateBackupExpire: "60d"
rotateBackupCompress: 9
rotateCheckInterval: "24h"
For detailed information on the logger
item, please refer to the section Logging - Configuration.