Nginx反向代理-传统单体项目相关配置

kyaa111 6月前 ⋅ 157 阅读

nginx.conf

server {
    listen 80;
    server_name blog.22xcode.com;
    #路径重写, http转https
    rewrite ^(.*)$  https://$host$1 permanent;
}

blog.conf

server {
    listen       443 ssl;
    server_name  blog.22xcode.com;
    # ssl证书配置
    ssl_certificate "blog.22xcode.com.pem";
    ssl_certificate_key "blog.22xcode.com.key";

    location / {
        #服务真实地址 后端服务无需设置https
        proxy_pass http://127.0.0.1:5151;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect default;
        set $mscheme $scheme;
        #传统项目中, 后端若返回302状态码, 跳转地址是使用http的, 所以如果nginx配置了https, 需要设置为https
        if ($http_referer ~* ^https.*) {
            set $mscheme "https";
        }
        proxy_set_header X-Forwarded-Proto $mscheme;
    }
}