Skip to main content

Implementing SCGI Support

1. Setting SCGI Pass

Directive Values Description
scgi_pass string Specifies the SCGI server address. Can be a domain name, IP address with port, or Unix socket path.

Example: Basic SCGI Pass Configuration

location /app/ {
    scgi_pass 127.0.0.1:9000;
}

Example: SCGI Pass with Unix Socket

location /app/ {
    scgi_pass unix:/var/run/scgi-app.socket;
}
Note: The scgi_pass directive is used to specify the SCGI server address. It can point to a TCP port or Unix socket, and must be set inside a location block.

2. Configuring SCGI Params

Parameter Description Default Notes
scgi_param Sets a parameter to be passed to the SCGI server None Can be used multiple times to define several parameters
scgi_pass Specifies the SCGI server address None Can be a domain name, IP address, or Unix socket
scgi_read_timeout Defines the read timeout for SCGI responses 60s Time to wait for a response from the SCGI server
scgi_connect_timeout Defines the connect timeout for SCGI connections 60s Time to wait for a connection to the SCGI server
scgi_send_timeout Defines the send timeout for SCGI requests 60s Time to wait for sending a request to the SCGI server

Example: Basic SCGI Configuration

location /app/ {
    scgi_pass unix:/var/run/scgi-app.socket;
    scgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    scgi_param QUERY_STRING $query_string;
    scgi_param REQUEST_METHOD $request_method;
    scgi_param CONTENT_TYPE $content_type;
    scgi_param CONTENT_LENGTH $content_length;
}

3. Including SCGI Config

Directive Description Example
scgi_pass Specifies the SCGI server address scgi_pass unix:/var/run/scgi-app.socket;
scgi_param Sets parameters to pass to the SCGI server scgi_param HTTP_X_CUSTOM_HEADER $http_x_custom_header;
scgi_connect_timeout Defines the timeout for connecting to the SCGI server scgi_connect_timeout 30s;
scgi_send_timeout Defines the timeout for sending a request to the SCGI server scgi_send_timeout 30s;
scgi_read_timeout Defines the timeout for reading a response from the SCGI server scgi_read_timeout 30s;

Example: Basic SCGI Configuration

location /scgi/ {
    scgi_pass unix:/var/run/scgi-app.socket;
    scgi_param PATH_INFO $fastcgi_path_info;
    scgi_param REQUEST_METHOD $request_method;
    scgi_param QUERY_STRING $query_string;
    scgi_param CONTENT_TYPE $content_type;
    scgi_param CONTENT_LENGTH $content_length;
}

4. Using TCP Socket

Directive Values Description
scgi_pass tcp://127.0.0.1:9000 Specifies the SCGI server address to which requests are forwarded.
scgi_bind 127.0.0.1:8080 Binds the client socket to a specific IP and port before connecting.
scgi_connect_timeout 60s Timeout for connecting to the SCGI server.
scgi_send_timeout 60s Timeout for sending a request to the SCGI server.
scgi_read_timeout 60s Timeout for reading a response from the SCGI server.

Example: Basic SCGI Configuration with TCP Socket

location /app {
    scgi_pass tcp://127.0.0.1:9000;
    scgi_connect_timeout 60s;
    scgi_send_timeout 60s;
    scgi_read_timeout 60s;
}
Note: Ensure that the SCGI server is running and accessible at the specified address.

5. Setting SCGI Timeouts

Directive Values Description
scgi_bind address Bind to a specific IP address and port for SCGI connections
scgi_connect_timeout time Timeout for connecting to SCGI server (default: 60s)
scgi_send_timeout time Timeout for sending request to SCGI server (default: 60s)
scgi_read_timeout time Timeout for reading response from SCGI server (default: 60s)
scgi_store_timeout time Timeout for storing response to file (default: 60s)

Example: Configuring SCGI Timeouts

location /app {
    scgi_pass unix:/var/run/scgi-app.socket;
    scgi_connect_timeout 30s;
    scgi_send_timeout 60s;
    scgi_read_timeout 60s;
}

6. Configuring SCGI Buffering

Directive Values Description
scgi_buffering on | off Enables or disables buffering of the response from the SCGI server. Default is on.
scgi_buffer_size size Sets the size of the buffer used for reading the first part of the response from the SCGI server. Default is 4k|8k.
scgi_buffers number size Defines the number and size of buffers used for reading the response from the SCGI server. Default is 8 4k|8k.
scgi_busy_buffers_size size Sets the maximum total size of buffers that can be busy sending a response to the client. Default is 2*4k|8k.
scgi_temp_path path Defines a directory for storing temporary files for the SCGI response. Default is /var/tmp.
scgi_temp_file_write_size size Sets the size of data written to a temporary file in one operation. Default is 1m.

Example: SCGI Buffering Configuration

location /app {
    scgi_pass unix:/var/run/scgi.sock;
    scgi_buffering on;
    scgi_buffer_size 8k;
    scgi_buffers 4 8k;
    scgi_busy_buffers_size 16k;
    scgi_temp_path /var/cache/nginx/scgi_temp;
}
Note: Buffering is recommended for performance, especially when dealing with large SCGI responses.

7. Setting SCGI Buffer Sizes

Directive Values Description
scgi_buffering on | off Enables or disables buffering of the response from the SCGI server. Default is on.
scgi_buffer_size size Defines the size of the buffer used for reading the first part of the SCGI response. Default is 4k or 8k.
scgi_buffers number size Defines the number and size of buffers used for reading SCGI response. Default is 8 4k or 8 8k.
scgi_busy_buffers_size size Defines the maximum size of buffers that can be filled at the same time. Default is 2 * scgi_buffer_size.
scgi_temp_path path Defines a directory for storing temporary files for SCGI responses. Default is /var/tmp.

Example: Configuring SCGI Buffer Sizes

location /app {
    scgi_pass scgi://127.0.0.1:9000;
    scgi_buffering on;
    scgi_buffer_size 8k;
    scgi_buffers 4 8k;
    scgi_busy_buffers_size 16k;
    scgi_temp_path /var/tmp/scgi_temp;
}

8. Handling SCGI Errors

Error Code Description Handling Strategy
500 Internal Server Error Check SCGI application logs for details. Ensure proper error handling in the application.
502 Bad Gateway Indicates SCGI server is not responding. Review SCGI configuration and server status.
503 Service Unavailable SCGI server is overloaded or unavailable. Implement retry logic or load balancing.
504 Gateway Timeout SCGI request timed out. Adjust timeout values in Nginx configuration.

Example: Error Page Configuration

location /scgi {
    scgi_pass unix:/var/run/scgi-app.sock;
    scgi_intercept_errors on;
    error_page 500 502 503 504 /scgi-error.html;
}
Note: Enabling scgi_intercept_errors allows Nginx to handle SCGI errors with custom error pages.

Was this article helpful?