1、概述html
項目總使用到Nginx的代理轉發,學習和整理內容以下,因爲是整理因此參考博客大牛的內容,有不少雷同之處,還望見諒(非抄襲對待)nginx
2、Nginx依賴包的安裝api
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
3、安裝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
安裝ui
//進入nginx目錄 cd /usr/local/nginx //執行命令 ./configure //執行make命令 make //執行make install命令 make install
Nginx經常使用命令atom
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主進程號
配置端口轉發url
配置config文件spa
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