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

  1. server:
  2. address: ":80"
  3. serverRoot: "/var/www/Server"

Later, you can use g.Server() to automatically obtain and set this configuration when acquiring the default singleton object.

Configuration - File - 图1warning

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:

  1. server:
  2. address: ":80"
  3. serverRoot: "/var/www/Server"
  4. server1:
  5. address: ":8080"
  6. serverRoot: "/var/www/Server1"
  7. server2:
  8. address: ":8088"
  9. serverRoot: "/var/www/Server2"

We can obtain the corresponding Server singleton object through the singleton object name:

  1. // Corresponding to server.server1 configuration item
  2. s1 := g.Server("server1")
  3. // Corresponding to server.server2 configuration item
  4. s2 := g.Server("server2")
  5. // Corresponding to default configuration item server
  6. s3 := g.Server("none")
  7. // Corresponding to default configuration item server
  8. s4 := g.Server()

Example 3, More Complete Example

For example, for the example in the previous section, the corresponding configuration file is as follows:

  1. server:
  2. address: ":8199"
  3. serverRoot: "/var/www/Server"
  4. indexFiles: ["index.html", "main.html"]
  5. accessLogEnabled: true
  6. errorLogEnabled: true
  7. pprofEnabled: true
  8. logPath: "/var/log/ServerLog"
  9. sessionIdName: "MySessionId"
  10. sessionPath: "/tmp/MySessionStoragePath"
  11. sessionMaxAge: "24h"
  12. 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:

  1. server:
  2. address: ":8199"
  3. serverRoot: "/var/www/Server"
  4. indexFiles: ["index.html", "main.html"]
  5. accessLogEnabled: true
  6. errorLogEnabled: true
  7. pprofEnabled: true
  8. log-path: "/var/log/ServerLog"
  9. session_Id_Name: "MySessionId"
  10. Session-path: "/tmp/MySessionStoragePath"
  11. session_MaxAge: "24h"
  12. DumpRouterMap: false

Configuration - File - 图2tip

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 includes Cookie data submitted by the client, default is 10KB.
  • ClientMaxBodySize: Body size limit submitted by the client, also affecting file upload size, default is 8MB.

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:

  1. server:
  2. maxHeaderBytes: "20KB"
  3. 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:

  1. server:
  2. address: ":8080"
  3. logger:
  4. path: "/var/log/server"
  5. file: "{Y-m-d}.log"
  6. stdout: false
  7. rotateSize: "100M"
  8. rotateBackupLimit: 10
  9. rotateBackupExpire: "60d"
  10. rotateBackupCompress: 9
  11. rotateCheckInterval: "24h"

For detailed information on the logger item, please refer to the section Logging - Configuration.