Example Nginx Configuration for Proxying Alibaba Cloud OSS Domain
This article provides a complete Nginx configuration example that proxies requests to an Alibaba Cloud OSS domain, adds the client’s real IP via the X-Real-IP header, enables gzip compression, and includes detailed comments and logging settings.
The following is a sample Nginx configuration that demonstrates how to proxy requests to an Alibaba Cloud OSS domain, with extensive inline comments explaining each directive.
<code># Define a new upstream named oss_backend
upstream oss_backend {
# Specify the Alibaba Cloud OSS domain
server oss-domain.aliyuncs.com;
}
# http block
http {
# Define log format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Access log path
access_log /var/log/nginx/access.log main;
# Error log path
error_log /var/log/nginx/error.log;
# Define http server
server {
# Listening port
listen 80;
# Server name
server_name example.com;
# Location block defining request handling rules
location / {
# Add a new header to pass the client’s real IP to the backend
proxy_set_header X-Real-IP $remote_addr;
# Proxy pass to the upstream oss_backend
proxy_pass http://oss_backend;
# Cache settings (off by default)
proxy_cache off;
# Timeout settings (default 60 seconds)
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# Disable backend redirects
proxy_redirect off;
# Buffer settings (default 4k or 8k)
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
# Enable gzip compression to improve transfer speed
gzip on;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/json;
# Allow only specific request methods
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
}
}
}</code>Replace the placeholder example.com with your own domain name. The configuration forwards client requests to the Alibaba Cloud OSS domain, adds the X-Real-IP header to convey the client’s actual IP address, and enables gzip compression for faster data transfer.
This is a basic example; for more advanced options and detailed explanations, refer to the official Nginx documentation.
Hope this helps!
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.