nginx转发HTTP配置-折腾笔记

有时候会有这种需求,需要将访问局域网内服务器的HTTP请求都经过某台机器做代理转发,以统计访问记录、记录错误等等等。

经过配置后,配置文件如下所示:

一、nginx.conf

user  www;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
#    include       /etc/nginx/mime.types;
#    default_type  application/octet-stream;
    include	/usr/local/nginx/conf/reverse-proxy.conf;
    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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    client_max_body_size 50m;
    client_body_buffer_size 256k;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    proxy_connect_timeout 300s; 
    proxy_read_timeout 300s;
    proxy_send_timeout 300s;
    proxy_buffer_size 256k;
    proxy_buffers 4 256k; 
    proxy_busy_buffers_size 256k; 
    proxy_temp_file_write_size 256k; 
    proxy_ignore_client_abort on; 
    #gzip  on;
    #iinclude /etc/nginx/conf.d/*.conf;
    server {
        listen 80;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
}
}

二、reverse-proxy.conf文件

将请求到本机的请求转发至192.168.1.123:80和192.168.1.124:8080。

server
{
    listen 80;
    server_name filedown.claves.me;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://192.168.1.123:80;
    }
    access_log /var/log/nginx/filedown.claves.mem.log;
}


server
{
    listen 80;
    server_name git.claves.mem;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://192.168.1.124:8080;
    }
    access_log /var/log/nginx/git.claves.me.log;
}

 

分享到:更多 ()