1.nginx的搭建依賴環境
1.1 准備jdk環境
當前最新版本下載地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html
歷史版本下載地址: http://www.oracle.com/technetwork/java/javase/archive-139210.html
1.2 解壓壓縮包
1.3 配置jdk環境變量,打開/etc/profile配置文件,將下面配置拷貝進去
#set java environment
JAVA_HOME=/usr/local/jdk1.7.0_71
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
1.4 重新加載/etc/profile配置文件 source /etc/profile
2.nginx的搭建
2.1 下載安裝包 http://nginx.org/download/ 並上傳
2.2 依賴環境
yum install gcc-c++
yum -y install pcre-devel openssl openssl-devel
2.3 解壓縮
2.4執行 ./configure --prefix=/usr/local/nginx
/usr/local/nginx為自定義的安裝地址
2.5 編譯 安裝
make && make install
2.6 進入自定義的安裝地址 /usr/local/nginx/sbin 啟動nginx
啟動:./nginx
關閉nginx:./nginx -s stop
重新加載配置文件:./nginx -s reload
2.7防火牆的設置
1:查看防火狀態
systemctl status firewalld
service iptables status
2:暫時關閉防火牆
systemctl stop firewalld
service iptables stop
3:永久關閉防火牆
systemctl disable firewalld
chkconfig iptables off
4:重啟防火牆
systemctl enable firewalld
service iptables restart
3.
4.
本地域名解析:
瀏覽器會首先在本機的hosts文件中查找域名映射的IP地址,如果查找到就返回IP ,沒找到則進行域名服務器解析,一般本地解析都會失敗,因為默認這個文件是空的
Windows下的hosts文件地址:C:/Windows/System32/drivers/etc/hosts
Linux下的hosts文件所在路徑: /etc/hosts
本地域名配置:
當瀏覽器中訪問的時 manage.leyou.com自動去找對應的Nginx中對應的ip地址
192.168.98.128 manage.leyou.com
192.168.98.128 api.leyou.com
Nginx中的配置
配置文件路徑: /usr/local/nginx/conf/nginx.conf
當找到對應的ip地址時,就回去找80端口的manage.leyou.com 反向代理轉發到http://192.168.11.82:9001
server {
listen 80;
server_name manage.leyou.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://192.168.11.82:9001;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
server {
listen 80;
server_name api.leyou.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://192.168.11.82:10010;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
反向代理配置解釋
5.訪問的過程
-
-
優先進行本地域名解析,因為我們修改了hosts,所以解析成功,得到地址:127.0.0.1
-
請求被發往解析得到的ip,並且默認使用80端口:http://127.0.0.1:80本機的nginx一直監聽80端口,因此捕獲這個請求
-
nginx中配置了反向代理規則,將manage.leyou.com代理到127.0.0.1:9001,因此請求被轉發
-
后台系統的webpack server監聽的端口是9001,得到請求並處理,完成后將響應返回到nginx
-
nginx將得到的結果返回到瀏覽器