網(wǎng)站404或空白怎么辦?
客戶反映:說自己的網(wǎng)站走nginx代理后,打開空白。直接IP加地址訪問是好的(http://ip:port)。
故障排查:
(1) 打開chrome瀏覽器,訪問了下,訪問情況真是客戶描述的那樣。
(2) 感覺打開chrome ,開發(fā)者工具,發(fā)現(xiàn)部分請求URL是404,css和js的
(3) 找客戶要服務(wù)器登錄的賬號,檢查nginx配置文件
- upstream www.test.com{
- server 127.0.0.1:8080;
- }
- server {
- listen 80;
- listen 443 ssl http2;
- ssl_certificate /usr/local/nginx/conf/ssl/www.test.com.pem;
- ssl_certificate_key /usr/local/nginx/conf/ssl/www.test.com.key;
- server_name www.test.com;
- access_log /data/wwwlogs/www.test.com_nginx.log combined;
- index index.html index.htm index.jsp;
- root /data/wwwroot/www.test.com;
- location ~ .*\.(js|css)?$ {
- expires 7d;
- access_log off;
- }
- location / {
- proxy_pass http://www.test.com;
- include proxy.conf;
- }
- }
(4) 大家有發(fā)現(xiàn)上面配置有問題不?剛開始我也沒有注意,自認(rèn)為配置文件是對 的。
打算檢查nginx的日志,一遍請求URL,一遍查看nginx果然還是404.(感覺疑惑),明明配置了proxy_pass http://www.test.com。
故障原因:
是因為 “location ~ .*\.(js|css)?$” 這個匹配攔截掉了,請求不能正常發(fā)往下一個“location /” ,也就不能正常抵達(dá)后端proxy_pass了。
解決方法:
第一種解決方法:是將后端的靜態(tài)文件(css 和js ),放入前置nginx 機器/data/wwwroot/www.test.com
第二種解決方法 :修改配置文件
- upstream www.test.com{
- server 127.0.0.1:8080;
- }
- server {
- listen 80;
- listen 443 ssl http2;
- ssl_certificate /usr/local/nginx/conf/ssl/www.test.com.pem;
- ssl_certificate_key /usr/local/nginx/conf/ssl/www.test.com.key;
- server_name www.test.com;
- access_log /data/wwwlogs/www.test.com_nginx.log combined;
- index index.html index.htm index.jsp;
- root /data/wwwroot/www.test.com;
- location ~ .*\.(js|css)?$ {
- proxy_pass http://www.test.com;
- expires 7d;
- access_log off;
- }
- location / {
- proxy_pass http://www.test.com;
- include proxy.conf;
- }
- }