溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nginx怎么配置反向代理

發(fā)布時(shí)間:2022-01-27 14:58:47 來源:億速云 閱讀:146 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“nginx怎么配置反向代理”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“nginx怎么配置反向代理”吧!

nginx怎么配置反向代理

準(zhǔn)備工作

系統(tǒng)環(huán)境:centos 6.5_64

關(guān)閉SELinux

臨時(shí)關(guān)閉:setenforce 0

永久關(guān)閉:永久關(guān)閉selinux 需要重啟系統(tǒng),所以可以先臨時(shí)關(guān)閉再修改配置文件。

[root@aliyun home] vim /etc/selinux/config#SELINUX=enforcing #注釋掉#SELINUXTYPE=targeted #注釋掉SELINUX=disabled #增加:wq  #保存,關(guān)閉。

放行防火墻80端口

[root@aliyun cnyunwei] /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@aliyun cnyunwei] /etc/init.d/iptables save #保存防火墻使配置生效

安裝編譯工具

[root@aliyun cnyunwei] yum -y  install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl

下載軟件

[root@aliyun cnyunwei] cd /home/pro #進(jìn)入目錄下載nginx
[root@aliyun cnyunwei] wget http://nginx.org/download/nginx-1.9.10.tar.gz

下載pcre (支持nginx偽靜態(tài))
[root@aliyun cnyunwei] wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz

下載ngx_cache_purge(清除指定URL緩存)
[root@aliyun cnyunwei] wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

解壓相關(guān)軟件包:

[root@aliyun cnyunwei] cd  /home/pro/
[root@aliyun pro] tar zxvf  pcre-8.37.tar.gz
[root@aliyun pro] tar zxvf  nginx-1.9.10.tar.gz
[root@aliyun pro] tar zxvf ngx_cache_purge-2.3.tar.gz

安裝pcre:

[root@aliyun pro] cd  pcre-8.37
[root@aliyun pcre-8.37] mkdir /usr/local/pcre
[root@aliyun pcre-8.37] ./configure --prefix=/usr/local/pcre #配置安裝路徑[root@aliyun pcre-8.37] make && make install

安裝nginx :

[root@aliyun pcre-8.37] groupadd nginx && useradd -r nginx -g nginx -s /sbin/nologin   #創(chuàng)建用戶和用戶組[root@aliyun pcre-8.37] cd /home/pro/nginx-1.9.10
[root@aliyun nginx-1.9.10] mkdir /usr/local/nginx
[root@aliyun nginx-1.9.10] ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/home/pro/pcre-8.37 --add-module=/home/pro/ngx_cache_purge-2.3
[root@aliyun nginx-1.9.10] make && make install

注意:–with-pcre=/home/pro/pcre-8.37 和–add-module=/home/pro/ngx_cache_purge-2.3 指向的是源碼包解壓的路徑,而不是安裝的路徑,否則會(huì)報(bào)錯(cuò)。

gx_cache_purge 是和nginx一起編譯安裝,不用單獨(dú)進(jìn)行安裝。

安裝完成后將系統(tǒng)服務(wù)文件nginx 復(fù)制到/etc/init.d/ 下。

[root@aliyun nginx-1.9.10] cd /etc/init.d/
[root@aliyun init.d] chmod 755 nginx     chown root.root nginx  #設(shè)置權(quán)限[root@aliyun init.d] chkconfig –add nginx  &&  chkconfig   nginx on  #添加到系統(tǒng)服務(wù)并設(shè)置自啟動(dòng)

配置都分別在對(duì)應(yīng)的文件中進(jìn)行加載,沒有寫入到主配置文件中:

[root@aliyun init.d] cd /usr/local/nginx/conf/
[root@aliyun conf] vim nginx.conf

配置文件信息如下:

#所屬用戶、用戶組user nginx nginx;#nginx進(jìn)程數(shù),建議設(shè)置為等于CPU總核心數(shù)worker_processes 1;
error_log /home/nginx/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {
use epoll;#單個(gè)進(jìn)程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進(jìn)程數(shù))worker_connections 65535;
}
http {#加載緩存include proxy.conf;#加載被代理服務(wù)器信息(可定義為負(fù)載均衡輪詢)include mysvrhost.conf;#加載mime類型include mime.types;#默認(rèn)文件類型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”‘;#access_log logs/access.log main;#服務(wù)器名字的hash表大小server_names_hash_bucket_size 128;#上傳文件大小限制client_header_buffer_size 32k;#設(shè)定請(qǐng)求緩存large_client_header_buffers 4 32k;
client_max_body_size 300m;#開啟高效文件傳輸模式sendfile on;
tcp_nopush on;#keepalive_timeout 0;#長(zhǎng)連接超時(shí)時(shí)間,單位是秒keepalive_timeout 60;#防止網(wǎng)絡(luò)阻塞tcp_nodelay on;#不顯示nginx版本信息server_tokens off;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;#禁止nginx空主機(jī)頭server {
listen 80 default;
server_name _;
location / {
root html;return 404;
}
location ~ /.ht {
deny all;
}
}#加載站點(diǎn)配置信息include /home/nginx/vhost/*.conf;
}

創(chuàng)建相關(guān)文件和目錄:

[root@aliyun conf] mkdir -p /home/nginx/proxy_temp_dir   #proxy_temp_dir與proxy_cache_dir這兩個(gè)文件夾必須在同一個(gè)分區(qū)[root@aliyun conf]  mkdir -p /home/nginx/proxy_cache_dir
[root@aliyun conf]  mkdir -p /home/nginx/vhost

創(chuàng)建緩存區(qū)配置文件:proxy.conf

[root@aliyun conf]  cd /usr/local/nginx/conf/
[root@aliyun conf]  vim proxy.conf#指定臨時(shí)文件目錄proxy_temp_path /home/nginx/proxy_temp_dir;#指定緩存區(qū)路徑,設(shè)置Web緩存區(qū)名稱為cache_one,內(nèi)存緩存為500MB,自動(dòng)清除1天內(nèi)沒有被訪問的文件,硬盤緩存為30GB。proxy_cache_path /home/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;#定義緩沖區(qū)代理緩沖客戶端請(qǐng)求的最大字節(jié)數(shù)client_body_buffer_size 512k;#定義連接后端服務(wù)器超時(shí)時(shí)間proxy_connect_timeout 60;#定義后端服務(wù)器響應(yīng)請(qǐng)求超時(shí)時(shí)間proxy_read_timeout 60;#定義后端服務(wù)器發(fā)送數(shù)據(jù)超時(shí)時(shí)間proxy_send_timeout 60;#定義代理請(qǐng)求緩存區(qū)大小proxy_buffer_size 32k;
proxy_buffers 4 64k;#定義系統(tǒng)繁忙時(shí)可申請(qǐng)的proxy_buffers大小proxy_busy_buffers_size 128k;#定義proxy緩存臨時(shí)文件的大小proxy_temp_file_write_size 128k;#定義故障轉(zhuǎn)移,如果后端的服務(wù)器返回502、504、執(zhí)行超時(shí)等錯(cuò)誤,自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移。proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;#定義使用web緩存區(qū)cache_oneproxy_cache cache_one;

創(chuàng)建upstream 被代理服務(wù)器文件:

[root@aliyun conf]  vim  mysvrhost.conf
upstream susecuihost {#每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,可以解決session的問題。ip_hash;#upstream的負(fù)載均衡,weight是權(quán)重,可以根據(jù)機(jī)器配置定義權(quán)重。weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大。#max_fails 允許請(qǐng)求失敗的次數(shù)默認(rèn)為1.當(dāng)超過最大次數(shù)時(shí),返回proxy_next_upstream 模塊定義的錯(cuò)誤。#fail_timeout 是max_fails次失敗后,暫停的時(shí)間。server 被代理服務(wù)器IP:80 weight=1 max_fails=2 fail_timeout=30s;#server 被代理服務(wù)器IP 2 :80 weight=1 max_fails=2 fail_timeout=30s;}

創(chuàng)建站點(diǎn)信息配置文件:

[root@aliyun conf] cd /home/nginx/vhost/
[root@aliyun vhost] vim  cnyunwei.conf
server {
listen 80;
server_name 站點(diǎn)域名;
location /
{#mysvrhost.conf 中配置的upstream 服務(wù)器池proxy_pass http://susecuihost;#增加設(shè)置web緩存的key值,nginx根據(jù)key值md5哈希存儲(chǔ)緩存proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;#后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IPproxy_set_header X-Forwarded-For $remote_addr;
proxy_cache_valid 200 304 12h;
expires 2d;
proxy_ignore_headers “Cache-Control” “Expires” “Set-Cookie”;
}#清除緩存(指定清除緩存參數(shù))location ~ /purge(/.*)
{#允許所有IP可清理緩存allow all;#allow 192.168.21.0/24;#deny all;proxy_cache_purge cache_one $host$1$is_args$args;
}#列出的擴(kuò)展名文件不緩存location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://susecuihost;
}
access_log off;
}

可以批量對(duì)使用到的文件重置下用戶組:

[root@aliyun conf]  chown nginx.nginx -R /home/nginx/proxy_cache_dir  /home/nginx/proxy_temp_dir  /home/nginx/vhost  
[root@aliyun conf]  service   nginx  start #啟動(dòng)nginx

感謝各位的閱讀,以上就是“nginx怎么配置反向代理”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)nginx怎么配置反向代理這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI