Nginx服務器如何搭建反向代理

第1部分:安裝

1 建立用戶及組

??/usr/sbin/groupadd?www? ??/usr/sbin/useradd?-g?www?www

2 安裝pcre 讓nginx反向代理支持rewrite 方便以后所需

??wget?ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz? ??tar?zxvf?pcre-7.8.tar.gz? ??cd?pcre-7.8/? ??./configure? ??make?&&?make?install

3 安裝nginx反向代理

??wget?http://sysoev.ru/nginx/nginx-0.7.58.tar.gz? ??tar?zxvf?nginx-0.7.58.tar.gz? ??cd?nginx-0.7.58/? ??./configure?--user=www?--group=www?--prefix=/usr/ ??local/webserver/nginx?--with-http_stub_status_module? ??--with-http_ssl_module?--with-cc-opt='-o2'?--with-cpu-opt ??=opteron? ??make?&&?make?install

注意上文中的–with-cc-opt=’-o2′ –with-cpu-opt=opteron 這是編譯器優化,目前最常用的是-02 而不是3.后面對應cpu的型號。

第2部分:配置及優化配置文件

1 nginx.conf 配置文件:

??user?www?www;? ??worker_processes?4;? ??#?[?debug?|?info?|?notice?|?warn?|?error?|?crit?]? ??error_log?/usr/local/webserver/nginx/logs/nginx_error.log?crit;? ??pid?/usr/local/webserver/nginx/nginx.pid;? ??#specifies?the?value?for?maximum?file?descriptors?that? ??can?be?opened?by?this?process.? ??worker_rlimit_nofile?51200;? ??events? ??{? ??use?epoll;? ??worker_connections?51200;? ??}? ??http? ??{? ??include?mime.types;? ??default_type?application/octet-stream;? ??source_charset?gb2312;? ??server_names_hash_bucket_size?256;? ??client_header_buffer_size?256k;? ??large_client_header_buffers?4?256k;? ??#size?limits? ??client_max_body_size?50m;? ??client_body_buffer_size?256k;? ??client_header_timeout?3m;? ??client_body_timeout?3m;? ??send_timeout?3m;? ??#參數都有所調整.目的是解決代理過程中出現的一些502?499錯誤?? ??sendfile?on;? ??tcp_nopush?on;? ??keepalive_timeout?120;?#參數加大,以解決做代理時502錯誤? ??tcp_nodelay?on;? ??include?vhosts/upstream.conf;? ??include?vhosts/bbs.linuxtone.conf;?? ??}

2 upstream.conf 配置文件(這也是做負載的配置方法

??upstream.conf? ??upstream?bbs.linuxtone.com?{? ??server?192.168.1.4:8099;? ??}

3 站點配置文件

??bbs.linuxtone.conf? ??server? ??{? ??listen?80;? ??server_name?bbs.linuxtone.conf;? ??charset?gb2312;? ??index?index.html?index.htm;? ??root?/date/wwwroot/linuxtone/;? ??location?~?^/nginxstatus/?{? ??stub_status?on;? ??access_log?off;? ??}? ??location?/?{? ??root?/date/wwwroot/linuxtone/;? ??proxy_redirect?off?;? ??proxy_set_header?host?$host;? ??proxy_set_header?x-real-ip?$remote_addr;? ??proxy_set_header?remote-host?$remote_addr;? ??proxy_set_header?x-forwarded-for?$proxy_add_x_forwarded_for;? ??client_max_body_size?50m;? ??client_body_buffer_size?256k;? ??proxy_connect_timeout?30;? ??proxy_send_timeout?30;? ??proxy_read_timeout?60;? ??proxy_buffer_size?256k;? ??proxy_buffers?4?256k;? ??proxy_busy_buffers_size?256k;? ??proxy_temp_file_write_size?256k;? ??proxy_next_upstream?error?timeout?invalid_header?http_500? ??http_503?http_404;? ??proxy_max_temp_file_size?128m;? ??proxy_pass?http://bbs.linuxtone.com;? ??}

參數都有所調整.目的是解決代理過程中出現的一些502 499錯誤

??#add?expires?header?for?static?content? ??location?~*?.(jpg|jpeg|gif|png|swf)$?{? ??if?(-f?$request_filename)?{? ??root?/date/wwwroot/linuxtone/;? ??expires?1d;? ??break;? ??}? ??}? ??log_format?access?'$remote_addr?-?$remote_user?[$time_local]?"$request"?'? ??'$status?$body_bytes_sent?"$http_referer"?'? ??'"$http_user_agent"?$http_x_forwarded_for';? ??access_log?/exp/nginxlogs/bbs.linuxtone_access.log?access;? ??}

常用指令
下面來看一些nginx的反向代理常用指令

proxy_pass指令
語法

??proxy_pass??[url?|?upstream]

作用
該指令用于設置被代理服務器端口或套接字,以及url

proxy_redirect指令
語法

??proxy_redirect??[off?|?default?|?redirect?replacement]

作用
該指令用于更改被代理服務器的應答header頭中的”location”和”refresh”
補充:
這個命令作用我還沒掌握,實際設置中都是off的,各位如果有了解的,歡迎博客下留言指導我

proxy_next_upstream指令
語法

復制代碼?代碼如下:

proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off]

作用
該指令用于設置當在哪種情況下,將請求轉發到下一臺服務器。在upstream負載均衡代理服務器池中,假設后端的一臺服務器無法訪問或返回指定錯誤響應代碼時,可以使用該指令將請求轉發到池中的下一臺服務器。
參數說明

??? error : 如果連接服務器時、發送請求時、讀取應答消息時發生錯誤?
??? timeout : 如果連接服務器時、傳遞請求時、讀取后端服務器應答消息時超時?
??? invalid_header : 后端服務器返回一個空的或錯誤的應答?
??? http_[500|502|503|504|404] : 后端服務器返回指定的應答狀態碼?
??? off : 禁止將請求轉發到下一臺后端服務器?

proxy_set_header指令
語法

??proxy_set_header?header?value

作用
該指令允許重新定義或添加header行到轉發給被代理服務器的請求信息中,它的值可以是文本,可以是變量,可以是文本和變量的組合

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享