環境:centos7.2html
node1:192.168.169.103(Nginx代理服務器,靜態資源服務器)node
Nginx版本:1.12.2nginx
node2:192.168.169.154(http服務器)web
實驗策略:node2的http服務,首頁爲index.html靜態頁,首先用Nginx反向代理node2的web服務,而後用location規則設置訪問HTML文件爲代理服務器的靜態資源路徑。vim
(1)node2首先建立http服務首頁,默認爲/var/www/html/路徑下的index.html,centos
cd /var/www/html瀏覽器
vim index.html服務器
this is http-server!!!app
#保存退出tcp
開啓node2 的http服務
systemctl start httpd
瀏覽器訪問node2的http服務
使用node1的Nginx反向代理node2的http服務
(2)在node1上:
安裝Nginx以後,開啓Nginx,訪問node1,初始頁面
查看主配置文件
cat /etc/nginx/nginx.conf #只粘貼出生效的配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
黃色部分爲會被加載的配置文件,即在/etc/nginx/conf.d下的全部以".conf"結尾命名的配置文件都會被加載。
紅色部分是設置監聽的server配置,能夠使用修改主配置文件或者在/etc/nginx/conf.d路徑下建立配置文件兩種方法進行代理設置。
方法一(修改主配置文件):
在紅色字段配置中的
location / {
}
修改成
location / {
proxy_pass http://192.168.169.154:80;
}
保存以後,從新加載Nginx,systemctl reload nginx
瀏覽器訪問node1的代理服務
代理成功,經過路徑方式訪問node2的index.htmll首頁
(3)訪問成功,而後設置全部的html文件都從Nginx代理服務器指定的目錄下讀取。
首先自定義一個放置HTML文件的目錄,定義在/opt/nginx下
mkdir /opt/nginx
vim index.html #建立一個與node2http服務相同名稱的index首頁
this is nginx_server!!!
#保存退出
修改主配置文件:
在剛配置location下再配置一個location:
location ~* \.html$ {
root /opt/nginx;
}
#保存退出
從新加載Nginx:systemctl reload nginx
查看代理:
經過路徑訪問代理服務器,與以前的路徑訪問對比:
訪問路徑成功被重定向到nginx指定的路徑下。
(4)爲實驗更加明顯
在node2的http服務網頁配置路徑下建立一個新文件
node2下:
cd /var/www/html/
vim 1.html #名字隨意
this is http_server test!
#保存退出
重啓http服務
訪問node2的http下的1.html文件
在node1上,首先在主配置文件中將以前配置的
location ~* \.html$ {
root /opt/nginx;
}
註釋掉。
而後路徑瀏覽器訪問node1的1.html,請求被分發到了node2的http服務下的1.html
而後再將node1下nginx的主配置文件的配置
location ~* \.html$ {
root /opt/nginx;
}
撤掉註釋。
從新加載nginx服務
從新路徑訪問node1代理服務的1.html頁面
發現出現404頁面找不到
再訪問index.html文件
發現關於訪問HTML頁面的請求被定向到了定義的路徑下,訪問node1下的1.html頁面出現404是由於沒有在指定的/opt/nginx路徑下找到1.html
在node1下的/opt/nginx路徑下建立
cd /opt/nginx/
vim 1.html
this is nginx_server test!
#保存退出
從新加載nginx服務
瀏覽器訪問node1的1.html頁面
規則配置成功。
(5)方法二(在/etc/nginx/conf.d下建立新的配置文件,文件名隨意,最好有辨識度,文件名後綴以.conf結尾便可):
在node1下:
修改主配置文件:
將如下關於server的配置註釋,文件是先加載主配置文件,再根據主配置文件的配置加載其餘文件,因此若是不註銷,以後的設置可能無效:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
#保存退出
cd /etc/nginx/conf.d
vim html.conf #簡單設置,出現效果便可,具體需求具體添加參數
server {
listen 80;
server_name _;
location / {
proxy_pass http://192.168.169.154:80;
}
}
#保存退出
將node2的http服務首頁內容改成this is the second Method!!!
http服務下的1.html內容修改成http-server second test!
從新啓動http服務並從新加載node1 的nginx服務
瀏覽器訪問node1代理:
在html.conf文件下繼續添加配置規則,在以前的location後邊添加便可
server {
listen 80;
server_name _;
location / {
proxy_pass http://192.168.169.154:80;
}
location ~* \.html$ {
root /opt/nginx;
}
}
#保存退出
從新加載
路徑訪問代理服務的1.html
測試成功。