Nginx 常用配置

注意#

主机映射#

map $http_host $served_host {
default $http_host;
beta.example.com www.example.com;
}
server {
# [...]
location / {
proxy_pass http://rubyapp.com;
proxy_set_header Host $served_host;
}
}

map $request_uri $redirect_uri {
~/(?<lang>(en|de|fr))/oldname /$lang/newname;
}
map $http_host $served_host {
default $http_host;
~(?<name>[^.]+).example.com $name.example.net;
}

子域名重写#

  • http://www.*.domain.com -> http://*.domain.com

#1

if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}

#2

server {
server_name www.domain.com;
rewrite ^ http://domain.com$request_uri permanent;
}

替代域名#

server
{
listen 80;
server_name a.com b.com c.com;
location ~* ^/comment/(.*) {
proxy_set_header HOST shared.com;
# $1 - stores capture from the location on top
# $is_args will return ? if there are query params
# $args stores query params
proxy_pass http://comment/$1$is_args$args;
}
}
server {
listen 80;
server shared.com;
location / {
# Proxy to some app handler
}
}
upstream comment {
server localhost; # or any other host essentially
}

基础配置#

user nginx;
worker_processes auto;
pcre_jit on;
error_log /var/log/nginx/error.log warn;
include /etc/nginx/modules/*.conf;
events {
worker_connections 1024;
}
# 路径重定向为参数
rewrite ^/article/(.*)$ /article.php?id=$1 last;

流配置#

stream {
upstream quic_upstreams {
server 10.66.2.1:443;
}
upstream http_upstreams {
server 10.66.2.1:80;
}
upstream https_upstreams {
server 10.66.2.1:443;
}
server {
listen 443 udp;
proxy_pass quic_upstreams;
proxy_timeout 1s;
proxy_responses 1;
error_log logs/udp.log;
}
server {
listen 80;
proxy_pass http_upstreams;
error_log logs/http.log;
}
server {
listen 443;
proxy_pass https_upstreams;
error_log logs/https.log;
}
}

Nginx 常用配置#

location / {
add_header Content-Type text/html;
return 200 'Glad you are here!';
}