nginx配置proxy_pass後,訪問時路徑丟失怎麼辦

Genee發表於2018-10-29

應用場景

當我訪問blog.first-blood.cn的時候,proxy_pass轉發到jonny023.github.io這個域名下去了,而jonny023.github.io/upload/hello.jpg這個檔案我通過blog.first-blood.cn/upload/hello.jpg訪問就出現404

解決辦法

  • 此時在nginx的配置檔案下的location配置下面新增一句
proxy_set_header Host jonny023.github.io;
複製程式碼
server {
	listen 80;
	server_name blog.first-blood.cn;
	charset utf-8,gbk;
	root html;  
	index index.html index.htm;
	location / {
		proxy_set_header Host jonny023.github.io;
		#proxy_set_header Host           $host;
		proxy_set_header X-Real-IP       $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass https://jonny023.github.io;
	}
	
	location ^~/archives/ {
		proxy_pass https://jonny023.github.io/archives/;
	}
	
	location ^~/uploads/ {
		proxy_pass https://jonny023.github.io/uploads/;
	}
	
	location /favico.ico {
		 proxy_pass https://jonny023.github.io;
		 charset utf-8,gbk;
		 expires      12h;
	}
	
	location ~ .*\.(js|css|eot|otf|ttf|woff|woff2)?$ {
		proxy_pass https://jonny023.github.io;
		charset utf-8,gbk;
		expires      12h;
	}
	
	location ~* \.(eot|otf|ttf|woff|woff2)$ {
		add_header Access-Control-Allow-Origin *;
	}
}
複製程式碼

相關文章