Nginx反向代理

罗小黑
2021-06-03 / 0 评论 / 33 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年06月04日,已超过63天没有更新,若内容或图片失效,请留言反馈。
  • nginx正向代理就是私网到公网
  • nginx反向代理就是公网到私网

获取请求网址

proxy_set_header Host $proxy_host;

代理服务器记录客户端IP

proxy_set_header X-Real_IP $remote_addr;

反向代理后服务器获取客户端真实IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
nginx代理与后端服务器连接超时时间(代理链接超时)
Syntax: proxy_connect_timeout time;

Default: proxy_connect_timeout 60s;

Context: http,server,location
nginx代理等待后端服务器的响应时间
Syntax: proxy_read_timeout time;

Default: proxy_read_timeout 60s;

Context: http,server,location

后端服务器数据回传给nginx代理超时时间

Syntax: proxy_send_timeout time;

Default: proxy_send_timeout 60s;

Context: http,server,location

proxy_buffer 代理缓冲区

nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传,不是全部接收完再传给客户端

Syntax: proxy_buffering on|off;

Default: proxy_buffering on;

Context: http,server,location

设置nginx代理保存用户头部信息缓存区大小

Syntax: proxy_buffer_size size;

Default: proxy_buffer_size 4k|8k;

Context: http,server,location
proxy_buffers 缓存区
Syntax: proxy_buffers number size;

Default: proxy_buffers 8 4k|8k;

Context: http,server,location
Proxy代理网站常用优化配置如下,将配置写入新文件,调用时使用include引用即可
vim /etc/nginx/proxy_params

proxy_set_header Host $proxy_host;

proxy_set_header X-Real_IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30s;

proxy_send_timeout 60s;

proxy_read_timeout 60s;


proxy_buffering on;

proxy_buffer_size 32k;

proxy_buffers 4 128K;

web服务器:

vim /etc/nginx/conf.d/blog.conf

server {
        listen 6.6.6.6:80;  //内网地址
        server_name blog.ucbk.cn;
        root /html/blog;
        index index.php index.html;

        location ~ \.php$ {
                root /html/blog;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                include proxy_params;
        }

}

nginx -t

systemctl restart nginx

代理服务器:

vim /etc/nginx/conf.d/blog.conf

server {
        listen 80;
        server_name blog.ucbk.cn;

        location / {
                proxy_pass http://6.6.6.6:80;
                include proxy_params;
        }

}


nginx -t

systemctl restart nginx 
1

评论 (0)

取消