nginx與apache配合反向 Proxy技術2

來源:互聯網
上載者:User

標籤:img   dex   conf   dup   htm   rem   for   頁面   com   

注意,上次我們只是簡單的在同一台伺服器類比搭建了一個新的http伺服器(啟用了8080連接埠),使用的是apache,從而類比了多台伺服器實現的Nginx反向 Proxy,通過Nginx向上遊Proxy 伺服器發送請求。

這一次對Nginx反向 Proxy做出具體的配置,按照《Mastering Nginx》推薦

首先

sudo su        //root模式

cd /etc/nginx/

touch ./proxy.conf        //建立一個單獨的代理配置

vim ./proxy.conf

如下:

#Mastering Nginx#關閉重新導向proxy_redirect off;#傳遞給上遊伺服器使用者頭資訊proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#不是嚴格的代理模組指令,如果設定的太低,將不能上傳檔案到上遊伺服器#注意上傳檔案大小要大於檔案系統檔案大小client_max_body_size 10m;client_body_buffer_size 128k;#Nginx等待時間proxy_connect_timeout 30;#Nginx與上遊伺服器兩次操作成功等待時間proxy_send_timeout 15;proxy_read_timeout 15;#這些緩衝控制了Nginx如何快速響應使用者請求proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;#控制worker進程阻塞後台資料的時間,值越大,處理阻塞時間越長proxy_temp_file_write_size 64k;

然後

vim ./sites-available/default        //編輯預設網站設定檔

location ~ ^/apache {        include proxy.conf;        proxy_pass http://localhost:8080;    }

service nginx reload         //重讀nginx

 

注意:如果nginx reload提示failed,請查看/var/log/nginx/error.log錯誤記錄檔!

接下來嘗試把上遊伺服器作為uploads目錄,專門存放使用者上傳的檔案。

vim /etc/nginx/sites-available/default

 include proxy.conf;    location /uploads {        client_max_body_size 500m;        proxy_connect_timeout 75;        proxy_send_timeout 90;        proxy_read_timeout 90;        proxy_pass http://localhost:8080;    }    location / {        index  index.htm index.html index.php;          #如果檔案不存在則嘗試TP解析          #如果請求既不是一個檔案,也不是一個目錄,則執行一下重寫規則        if (!-e $request_filename)        {            #地址作為將參數rewrite到index.php上。            rewrite ^/(.*)$ /index.php/$1;        }    }

注意把proxy.conf包含在location外部,然後內部覆蓋原有的配置,否則會提示“directive is duplicate”。

 

再次訪問:www.gxy.gift/uploads

apache將會代理隱藏檔的訪問

 

接下來講解反向 Proxy中結合try_files提示:

在《Mastering Nginx》一書中提到這段代碼的作用:

location / {        try_files $uri @apache;    }    location @apache {        proxy_pass http://localhost:8080;    }

這段配置非常簡單,簡單說:請求發送給Nginx伺服器後,按照請求的檔案或者路由活取nginx的檔案,從而呈現出靜態頁面,但是如果Nginx找不到這樣的地址檔案或者無法路由,就會把地址直接丟給上遊的@apache

 

總結:所謂web反向 Proxy服務,要注意Proxy 伺服器使用過程中是直接把網域名稱後面的這一段請求,也就是$uri發送到上遊伺服器,在這兒是apache,相當於使用者直接請求了apache伺服器的效果,如果apache伺服器也不能處理這段請求,那麼將會顯示apache的not found提示。

 

nginx與apache配合反向 Proxy技術2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.