nginx

package
v1.9.10-0...-ff2253c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Debug = iota
	Info
	Notice
	Warn
	Error
	Crit
	Alert
	Emerg
)

The values below must stay aligned with the indexes of logLevel, because GetLogLevel returns an index into that slice.

View Source
const (
	// AccessLogRegexPattern matches access_log directive with unquoted path
	// Matches: access_log /path/to/file
	AccessLogRegexPattern = `(?m)^\s*access_log\s+([^\s;]+)`

	// ErrorLogRegexPattern matches error_log directive with unquoted path
	// Matches: error_log /path/to/file
	ErrorLogRegexPattern = `(?m)^\s*error_log\s+([^\s;]+)`

	// PIDRegexPattern matches pid directive with unquoted path
	// Matches: pid /path/to/file;
	PIDRegexPattern = `(?m)^\s*pid\s+([^\s;]+)`
)

Regular expressions for parsing directives from nginx -T output

View Source
const (
	Server   = "server"
	Location = "location"
	Upstream = "upstream"
	Http     = "http"
	Stream   = "stream"
)
View Source
const (
	ModuleStream = "stream"
)
View Source
const Unknown = -1

Variables

View Source
var (
	ErrNginx             = e.New(50000, "nginx error: {0}")
	ErrBlockIsNil        = e.New(50001, "block is nil")
	ErrReloadFailed      = e.New(50002, "reload nginx failed: {0}")
	ErrNginxTOutputEmpty = e.New(50003, "nginx -T output is empty")
)
View Source
var ErrControlOperationRunning = errors.New("another nginx control operation is already running")

Functions

func ClearModulesCache

func ClearModulesCache()

ClearModulesCache clears the modules cache (public version for external use)

func DirectiveValues

func DirectiveValues(content, directive string) ([]string, error)

func FmtCode

func FmtCode(content string) (fmtContent string, err error)

func GetAccessLogPath

func GetAccessLogPath() (path string)

GetAccessLogPath returns the path of the nginx access log file

func GetConfEntryPath

func GetConfEntryPath() (path string)

GetConfEntryPath returns the absolute path to the main nginx.conf. It prefers the value from `nginx -V --conf-path=...`. If that can't be parsed, it falls back to "<confDir>/nginx.conf".

func GetConfNameBySymlinkName

func GetConfNameBySymlinkName(name string) string

func GetConfPath

func GetConfPath(dir ...string) (confPath string)

GetConfPath returns the nginx configuration directory (e.g. "/etc/nginx"). It tries to derive it from `nginx -V --conf-path=...`. If parsing fails, it falls back to a reasonable default instead of returning "".

func GetConfSymlinkPath

func GetConfSymlinkPath(path string) string

func GetDirectives

func GetDirectives() (map[string]Directive, error)

func GetErrorLogPath

func GetErrorLogPath() string

GetErrorLogPath returns the path of the nginx error log file

func GetLoadModuleRegex

func GetLoadModuleRegex() *regexp.Regexp

GetLoadModuleRegex returns a compiled regular expression to match nginx load_module statements. It matches both quoted and unquoted module paths:

  • load_module "/usr/local/nginx/modules/ngx_stream_module.so";
  • load_module modules/ngx_http_upstream_fair_module.so;

The regex captures the module name (without path and extension).

func GetLogLevel

func GetLogLevel(output string) (level int)

func GetModules

func GetModules() *orderedmap.OrderedMap[string, *Module]

func GetModulesPath

func GetModulesPath() string

GetModulesPath returns the path of the nginx modules

func GetNginxExeDir

func GetNginxExeDir() string

GetNginxExeDir Returns the directory containing the nginx executable

func GetPIDPath

func GetPIDPath() (path string)

GetPIDPath returns the nginx master process PID file path. Resolution order:

  1. User override via settings (PIDPath)
  2. Runtime override from `nginx -T` for external containers
  3. Compile-time default from `nginx -V --pid-path=...`
  4. Probing common candidate paths (Docker-aware)

func GetPrefix

func GetPrefix() string

GetPrefix returns the prefix of the nginx executable

func GetSbinPath

func GetSbinPath() (path string)

GetSbinPath returns the path of the nginx executable

func GetStatusSnapshot

func GetStatusSnapshot() (*ControlResult, *ControlOperation)

GetStatusSnapshot returns the command result and operation from the same state revision.

func IsModuleLoaded

func IsModuleLoaded(module string) bool

IsModuleLoaded checks if a module is loaded in Nginx

func IsRunning

func IsRunning() bool

func Reload

func Reload() (stdOut string, stdErr error)

Reload reloads the nginx

func Restart

func Restart()

Restart restarts the nginx

func RewriteDirectiveValues

func RewriteDirectiveValues(content string, replacements []DirectiveValueReplacement) (string, int, error)

func TestConfig

func TestConfig() (stdOut string, stdErr error)

TestConfig tests the nginx config

Types

type ControlFunc

type ControlFunc func() (stdOut string, stdErr error)

type ControlOperation

type ControlOperation struct {
	ID         string                `json:"id"`
	Action     string                `json:"action"`
	State      ControlOperationState `json:"state"`
	StartedAt  time.Time             `json:"started_at"`
	FinishedAt *time.Time            `json:"finished_at,omitempty"`
	Message    string                `json:"message,omitempty"`
	Level      int                   `json:"level"`
	ExitCode   *int                  `json:"exit_code,omitempty"`
}

func GetControlOperation

func GetControlOperation() *ControlOperation

GetControlOperation returns a snapshot of the latest tracked control operation.

func StartRestart

func StartRestart(operationID string) (*ControlOperation, error)

StartRestart starts a tracked asynchronous restart operation.

type ControlOperationState

type ControlOperationState string
const (
	ControlOperationRunning   ControlOperationState = "running"
	ControlOperationSucceeded ControlOperationState = "succeeded"
	ControlOperationFailed    ControlOperationState = "failed"
)

type ControlResp

type ControlResp struct {
	Message string `json:"message"`
	Level   int    `json:"level"`
}

type ControlResult

type ControlResult struct {
	// contains filtered or unexported fields
}

func Control

func Control(controlFunc ControlFunc) *ControlResult

func GetLastResult

func GetLastResult() *ControlResult

GetLastResult returns the last output of the nginx command

func (*ControlResult) GetError

func (t *ControlResult) GetError() error

func (*ControlResult) GetLevel

func (t *ControlResult) GetLevel() int

func (*ControlResult) GetOutput

func (t *ControlResult) GetOutput() string

func (*ControlResult) GetStdErr

func (t *ControlResult) GetStdErr() error

func (*ControlResult) GetStdOut

func (t *ControlResult) GetStdOut() string

func (*ControlResult) IsError

func (t *ControlResult) IsError() bool

func (*ControlResult) Resp

func (t *ControlResult) Resp(c *gin.Context)

func (*ControlResult) RespError

func (t *ControlResult) RespError(c *gin.Context)

type Directive

type Directive struct {
	Links []string `json:"links"`
}

type DirectiveValueReplacement

type DirectiveValueReplacement struct {
	Directive string
	OldValue  string
	NewValue  string
}

type ErrorCategory

type ErrorCategory string
const (
	ErrorCategoryNone              ErrorCategory = ""
	ErrorCategoryMissingInclude    ErrorCategory = "missing_include"
	ErrorCategorySandboxBuildError ErrorCategory = "sandbox_build_error"
	ErrorCategorySyntaxError       ErrorCategory = "syntax_error"
	ErrorCategoryNginxRuntimeError ErrorCategory = "nginx_runtime_error"
)

func DetectErrorCategory

func DetectErrorCategory(message string, stdErr error) ErrorCategory

type Module

type Module struct {
	Name    string `json:"name"`
	Params  string `json:"params,omitempty"`
	Dynamic bool   `json:"dynamic"`
	Loaded  bool   `json:"loaded"`
}

type NamespaceInfo

type NamespaceInfo struct {
	ID         uint64
	Name       string
	DeployMode string
}

NamespaceInfo represents minimal namespace info for sandbox

type NgxConfig

type NgxConfig struct {
	FileName  string         `json:"file_name"`
	Name      string         `json:"name"`
	RootBlock string         `json:"root_block,omitempty"`
	Upstreams []*NgxUpstream `json:"upstreams"`
	Servers   []*NgxServer   `json:"servers"`
	Custom    string         `json:"custom"`
	// contains filtered or unexported fields
}

func NewNgxConfig

func NewNgxConfig(filename string) *NgxConfig

func ParseNgxConfig

func ParseNgxConfig(filename string) (ngxConfig *NgxConfig, err error)

func ParseNgxConfigByContent

func ParseNgxConfigByContent(content string) (ngxConfig *NgxConfig, err error)

func (*NgxConfig) BuildConfig

func (c *NgxConfig) BuildConfig() (content string, err error)

func (*NgxConfig) FmtCode

func (c *NgxConfig) FmtCode() (fmtContent string)

type NgxDirective

type NgxDirective struct {
	Directive string `json:"directive"`
	Params    string `json:"params"`
	Comments  string `json:"comments"`
	// Raw, when non-empty, is the verbatim source text of the directive (including
	// any block body). BuildConfig prefers Raw over Directive/Params so that block
	// directives (e.g. ssl_certificate_by_lua_block) and quoted parameters survive
	// a maintenance-config rebuild without being flattened. Keep Directive and
	// Params populated for callers that consume this struct via JSON.
	Raw string `json:"-"`
}

func (*NgxDirective) Orig

func (d *NgxDirective) Orig() string

func (*NgxDirective) ParseDirective

func (d *NgxDirective) ParseDirective(directive config.IDirective, deep int)

func (*NgxDirective) TrimParams

func (d *NgxDirective) TrimParams()

type NgxLocation

type NgxLocation struct {
	Path     string `json:"path"`
	Content  string `json:"content"`
	Comments string `json:"comments"`
}

func (*NgxLocation) ParseLocation

func (l *NgxLocation) ParseLocation(directive config.IDirective, deep int)

type NgxServer

type NgxServer struct {
	Directives []*NgxDirective `json:"directives"`
	Locations  []*NgxLocation  `json:"locations"`
	Comments   string          `json:"comments"`
}

func NewNgxServer

func NewNgxServer() *NgxServer

func (*NgxServer) ParseServer

func (s *NgxServer) ParseServer(directive config.IDirective)

type NgxUpstream

type NgxUpstream struct {
	Name       string          `json:"name"`
	Directives []*NgxDirective `json:"directives"`
	Comments   string          `json:"comments"`
}

type Sandbox

type Sandbox struct {
	Dir        string
	ConfigPath string
	Namespace  *NamespaceInfo
}

Sandbox represents an isolated nginx test environment

func (*Sandbox) Cleanup

func (s *Sandbox) Cleanup()

Cleanup removes the sandbox directory

type SandboxBuildError

type SandboxBuildError struct {
	Category ErrorCategory
	Message  string
}

func (*SandboxBuildError) Error

func (e *SandboxBuildError) Error() string

type SandboxReason

type SandboxReason string
const (
	SandboxReasonNone              SandboxReason = ""
	SandboxReasonRemoteNamespace   SandboxReason = "remote_namespace"
	SandboxReasonSeparateContainer SandboxReason = "separate_container"
	SandboxReasonCustomTestCommand SandboxReason = "custom_test_command"
)

type SandboxStatus

type SandboxStatus string
const (
	SandboxStatusOK      SandboxStatus = "ok"
	SandboxStatusSkipped SandboxStatus = "skipped"
	SandboxStatusFailed  SandboxStatus = "failed"
)

type TestConfigResult

type TestConfigResult struct {
	Message       string        `json:"message"`
	Level         int           `json:"level"`
	TestScope     TestScope     `json:"test_scope"`
	SandboxStatus SandboxStatus `json:"sandbox_status,omitempty"`
	SandboxReason SandboxReason `json:"sandbox_reason,omitempty"`
	ErrorCategory ErrorCategory `json:"error_category,omitempty"`
}

func NewSandboxBuildFailureResult

func NewSandboxBuildFailureResult(err error) TestConfigResult

func NewTestConfigResult

func NewTestConfigResult(stdOut string, stdErr error, scope TestScope, sandboxStatus SandboxStatus) TestConfigResult

func SandboxTestConfigWithPaths

func SandboxTestConfigWithPaths(namespace *NamespaceInfo, sitePaths, streamPaths []string) TestConfigResult

SandboxTestConfigWithPaths tests nginx config in an isolated sandbox with provided paths.

type TestScope

type TestScope string
const (
	TestScopeGlobal           TestScope = "global"
	TestScopeNamespaceSandbox TestScope = "namespace_sandbox"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL