Nginx中實現反向 Proxy

來源:互聯網
上載者:User

Nginx的2種用途

靜態內容的web伺服器;

反向 Proxy伺服器;

Nginx作為反向 Proxy的特點

接收使用者請求是非同步,即先將使用者請求全部接收下來,再一次性發送後後端web伺服器,極大的減輕後端web伺服器的壓力;

nginx代理和後端web伺服器間無需長串連;

發送響應報文時,是邊接收來自後端web伺服器的資料,邊發送給用戶端的;

涉及的模組

Proxy:標準的HTTP模組,實現反向 Proxy功能

Upstream:標準的HTTP模組,對後端web伺服器調度做負載平衡功能;

FastCGI:標準HTTP模組,將php動態請求代理至後端PHP伺服器;

配置部署

說明:本篇中,Nginx負責靜態訪問處理,動態訪問將會被代理至後端PHP伺服器;

# vi  /etc/nginx/nginx.htmlworker_processes  2;pid    /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    sendfile        on;    keepalive_timeout  65;    upstream backend {        server    172.16.25.112:9000;    }    server {        listen          80;        server_name     xxrenzhe.lnmmp.com;        access_log/var/log/nginx/lnmmp.access.log;        error_log/var/log/nginx/lnmmp.errors.log notice;        root           /www/lnmmp.com;        location / {            try_files $uri @missing; # 先訪問本地的靜態資源,若失敗,則轉入missing處理塊;        }        location @missing {            rewrite ^(.*[^/])$ $1/ permanent; # 直接存取網域名稱或IP地址時,在其後增加結尾符/,並返回301            rewrite ^ /index.php last; # 將始終無法訪問到的資源(如404錯誤),全部重新導向至首頁        }        # 禁止system目錄訪問,但允許指定類型的靜態檔案訪問        location ~* ^/system/.+\.(jpg|jpeg|png|gif|css|js|swf|flv|ico)$ {            expires max;            tcp_nodelay off;            tcp_nopush on;        }        # 訪問/system/時,則直接跳回首頁        location ~ /system/ {            rewrite ^ /index.php last;        }        location ~* \.php$ {            default_type text/html;            charset      utf-8;            fastcgi_pass backend;            fastcgi_index index.php;            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;            include fastcgi_params;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.