- 在/usr/local/nginx/static下創建2個文件夾(web應用根目錄):
- 修改nginx.conf配置:
配置負載均衡服務器列表localweb
通過proxy_pass將請求轉向localweb定義的服務器列表實現動態資源請求的負載均衡
通過以下配置實現多個web站點靜態資源配置
location ~ /web1{
root static;
}
location ~ /web2{
root static;
}
完整的配置如下:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
client_max_body_size 40m;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#配置負載均衡服務器列表
upstream localweb{
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localweb; #請求轉向localweb定義的服務器列表
}
# location /web0 {
# proxy_pass http://localweb/web0; #請求轉向localweb定義的服務器列表
# }
#不採用後綴,因爲文件下載接口也可能帶這些後綴
# location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
# root /usr/local/nginx/static;
# }
location ~ /web1{
root static;
}
location ~ /web2 {
root static;
}
}
}
注意點:前端頁面的資源引用路徑不能使用絕對路徑,應該使用相對路徑,相對根目錄。