跳到主要內容

Use Nginx as a Reverse Proxy to serve multiple web sites



Due to save IPs, I tried to use Nginx as a reverse proxy to serve multiple web sites.
server {
    listen 80;
    server_name server1.mydomain.com;

    location / {
        proxy_pass http://10.10.10.1/; #server1 internal IP
    }
}

This will cause 403 Forbidden error. I found the answer, add three lines:

server {
    listen 80;
    server_name server1.mydomain.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://10.10.10.1/; #server1 internal IP
    }
}

When I restart Nginx, will cause a warning:
[emerg]: could not build the proxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512 or proxy_headers_hash_bucket_size: 64

I found this solution. Finally I modified the config to this:

server {
    listen 80;
    server_name server1.mydomain.com;

    location / {
        proxy_headers_hash_max_size 51200;
        proxy_headers_hash_bucket_size 6400;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://10.10.10.1/; #server1 internal IP
    }
}

Everythins is fine!

Notice:
    if use ssl, every site sould use its own certificate, for example:

 server {
        listen       443 ssl;
        server_name  server1.mydomain.com;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_headers_hash_max_size 51200;
        proxy_headers_hash_bucket_size 6400;
        ssl_certificate      /etc/ssl/server1.mydomain.com.crt;
        ssl_certificate_key  /etc/ssl/server1.mydomain.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
           proxy_pass http://10.10.10.1/; #server1 internal IP
    }
}

留言

這個網誌中的熱門文章

自然人憑證讀卡機驅動程式

鳥毅用的是第一代的自然人憑證讀卡機,EZ100PU(後來有同事買EZmini可以讀SIM卡似乎更好),每年報稅時用一次。 本來只是要申請些政府業務,一時之間找不到光碟,沒想到在 驅動程式下載 居然看到Linux和Mac的驅動程式,剩下的就是政府單位的網頁和程式應該改版了吧!!!

用ZedGraph畫統計圖

Update: 沒想到這篇居然變成Google搜尋ZedGraph第一篇中文網頁,不過還是誠心建議用Windows上的C#先看一下 免費的圖表元件:Microsoft Chart Controls ,除非你非得用.Net 2.0(Windows 2000)或是用 Mono 。 BTW,我並不想成為微軟MVP,所以本Blog並不是有問必答的喲^_^ 才剛貼完上一篇,馬上就有位朋友丟過來一個LGPL Open Source元件的網址: ZedGraph 。 參考: A flexible charting library for .NET