跳转至

常用的配置

去除前缀

rewrite "/ai/logging($|/)(.*)" /$2 break;

四层代理

yum 安装包

yum install -y nginx nginx-all-modules.noarch 

安装完成后查看已经安装的包

> yum list installed | grep nginx
nginx.x86_64                      1:1.16.1-3.el7                 @epel
nginx-all-modules.noarch          1:1.16.1-3.el7                 @epel
nginx-filesystem.noarch           1:1.16.1-3.el7                 @epel
nginx-mod-http-image-filter.x86_64
nginx-mod-http-perl.x86_64        1:1.16.1-3.el7                 @epel
nginx-mod-http-xslt-filter.x86_64 1:1.16.1-3.el7                 @epel
nginx-mod-mail.x86_64             1:1.16.1-3.el7                 @epel
nginx-mod-stream.x86_64           1:1.16.1-3.el7                 @epel

启动 nginx,设置 nginx 开机自启动

systemctl start nginx

systemctl enable nginx

nginx 转发的配置如下:

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;


events {
    worker_connections 1024;
}

stream {

    log_format proxy '$remote_addr [$time_local]'
                    '$protocol $status $bytes_sent $bytes_received'
                    '$session_time "$upstream_addr" '
                    '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    access_log /var/log/nginx/tcp-access.log proxy ;
    error_log  /var/log/nginx/tcp-error.log warn ;

    upstream kube80 {
        server  127.0.0.1:10080 max_fails=3 fail_timeout=30s;
    }
    upstream kube443 {
        server  127.0.0.1:10443 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 443;
        proxy_connect_timeout 2s;
        proxy_timeout 9000s;
        proxy_pass kube443;
    }
    server {
        listen 80;
        proxy_connect_timeout 2s;
        proxy_timeout 9000s;
        proxy_pass kube80;
    }

}

从反向代理提供错误页面,而不是来自上游服务器

如果将 Nginx 作为反向代理运行,并且希望从反向代理本身提供错误页面,需要进行以下代理设置:

proxy_intercept_errors on;

没有这个,Nginx 会将来自 upstream 的错误页面转发给客户端。

error_page 400 /400.html;
location /400.html {
    root   /var/www/error_pages;
    internal;
}

error_page 500 /500.html;
location /500.html {
    root   /var/www/error_pages;
    internal;
}

error_page 502 /502.html;
location /502.html {
    root   /var/www/error_pages;
    internal;
}

error_page 503 /503.html;
location /503.html {
    root   /var/www/error_pages;
    internal;
}

error_page 504 /504.html;
    location /504.html {
    root   /var/www/error_pages;
    internal;
}