Centos安裝Nginx(轉載)
一、概述
專案總使用到Nginx的代理轉發,學習和整理內容如下,由於是整理所以參考部落格大牛的內容,有很多雷同之處,還望見諒(非抄襲對待)
二、Nginx依賴包的安裝
yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel //一鍵安裝上面四個依賴 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
三、安裝Nginx
下載:
//建立一個資料夾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
安裝
//進入nginx目錄 cd /usr/local/nginx //執行命令 ./configure //執行make命令 make //執行make install命令 make install
Nginx常用命令
cd /user/local/nginx/config #配置檔案路徑
//測試配置檔案 安裝路徑下的/nginx/sbin/nginx -t 複製程式碼//啟動命令 安裝路徑下的/nginx/sbin/nginx //停止命令 安裝路徑下的/nginx/sbin/nginx -s stop 或者 : nginx -s quit //重啟命令 安裝路徑下的/nginx/sbin/nginx -s reload 複製程式碼 //檢視程序命令 ps -ef | grep nginx //平滑重啟 kill -HUP Nginx主程序號
配置埠轉發
配置config檔案
server { listen 9100; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ^~/api/datacheck/ { proxy_redirect off; proxy_set_header Host $host:9106; 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 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時,如果是按照^~匹配路徑時,要注意proxy_pass後的url最後的/,當加上了/,相當於是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分也給代理走。
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
如上面的配置,如果請求的url是http://servername/static_js/test.html
會被代理成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;
}
則會被代理到http://js.test.com/static_js/test.htm
當然,我們可以用如下的rewrite來實現/的功能
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)//1 break;
proxy_pass http://js.test.com;
}
參考地址:
非Centos下Nginx安裝: https://www.cnblogs.com/taiyonghai/p/6728707.html