Centos安裝Nginx(轉(zhuǎn)載)
一、概述
項(xiàng)目總使用到Nginx的代理轉(zhuǎn)發(fā),學(xué)習(xí)和整理內(nèi)容如下,由于是整理所以參考博客大牛的內(nèi)容,有很多雷同之處,還望見諒(非抄襲對(duì)待)
二、Nginx依賴包的安裝
yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel //一鍵安裝上面四個(gè)依賴 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
三、安裝Nginx
下載:
//創(chuàng)建一個(gè)文件夾 cd /usr/local mkdir nginx cd nginx //下載tar包 wget http://nginx.org/download/nginx-1.13.7.tar.gz tar -xvf nginx-1.13.7.tar.g
安裝
//進(jìn)入nginx目錄 cd /usr/local/nginx //執(zhí)行命令 ./configure //執(zhí)行make命令 make //執(zhí)行make install命令 make install
Nginx常用命令
cd /user/local/nginx/config #配置文件路徑
//測(cè)試配置文件 安裝路徑下的/nginx/sbin/nginx -t 復(fù)制代碼 //啟動(dòng)命令 安裝路徑下的/nginx/sbin/nginx //停止命令 安裝路徑下的/nginx/sbin/nginx -s stop 或者 : nginx -s quit //重啟命令 安裝路徑下的/nginx/sbin/nginx -s reload 復(fù)制代碼 //查看進(jìn)程命令 ps -ef | grep nginx //平滑重啟 kill -HUP Nginx主進(jìn)程號(hào)
配置端口轉(zhuǎn)發(fā)
配置config文件(內(nèi)容不全)
#socket端口的轉(zhuǎn)發(fā):注意舊Nginx版本不支持 stream { #EMAIL-Nginx upstream emailServer { server 192.168.1.200:1433 weight=1 max_fails=2 fail_timeout=30s; } server { listen 1433; proxy_connect_timeout 60; proxy_timeout 60; proxy_pass emailServer; } } #http請(qǐng)求的轉(zhuǎn)發(fā) http { server { listen 9100; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ^~/api/datacheck/ { proxy_redirect off; #保留代理之前的host 包含客戶端真實(shí)的域名和端口號(hào) proxy_set_header Host $host; #保留代理之前的真實(shí)客戶端ip proxy_set_header X-Real-IP $remote_addr; #這個(gè)Header和X-Real-IP類似,但它在多級(jí)代理時(shí)會(huì)包含真實(shí)客戶端及中間每個(gè)代理服務(wù)器的IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 256k; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 4k; proxy_buffers 8 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_pass http://127.0.0.1::8080 } } }
在nginx中配置proxy_pass時(shí),如果是按照^~匹配路徑時(shí),要注意proxy_pass后的url最后的/,當(dāng)加上了/,相當(dāng)于是絕對(duì)根路徑,則nginx不會(huì)把location中匹配的路徑部分代理走;如果沒有/,則會(huì)把匹配的路徑部分也給代理走。
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
如上面的配置,如果請(qǐng)求的url是http://servername/static_js/test.html
會(huì)被代理成http://js.test.com/test.html
而如果這么配置
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}
則會(huì)被代理到http://js.test.com/static_js/test.htm
當(dāng)然,我們可以用如下的rewrite來實(shí)現(xiàn)/的功能
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)<span id="MathJax-Span-2" class="mrow"><span id="MathJax-Span-3" class="texatom"><span id="MathJax-Span-4" class="mrow"><span id="MathJax-Span-5" class="mo">//1 break;
proxy_pass http://js.test.com;
}
參考地址:
非Centos下Nginx安裝: http://www.rzrgm.cn/taiyonghai/p/6728707.html
nginx默認(rèn)安裝(yum) ,安裝完成后目錄和程序
cd /etc/nginx
cd /varlog/nginx

浙公網(wǎng)安備 33010602011771號(hào)