首页 新闻 会员 周边 捐助

Centos 6.5 安装nginx + Tomcat 不显示图片问题

0
悬赏园豆:10 [已解决问题] 解决于 2017-12-11 08:19

小白一个 在服务器上安装了nginx 和tomcat 放上一个欢迎页

我用nginx 监听80端口 用nginx 跳转到8080端口下的wzpbk项目不显示图片

www.wzpbk.com  //不显示图片  我"认为"这个直接去访问80端口然后经过nginx去访问80端口不加载图片

 

www.wzpbk.com:8080/wzpbk/   //显示图片 我"认为"这个是直接访问了tomcat 没经过nginx所以加载了图片

 

下面贴出nginx.conf文件

  1 #user  nobody;
  2 user root;
  3 worker_processes  1;
  4 
  5 #error_log  logs/error.log;
  6 #error_log  logs/error.log  notice;
  7 #error_log  logs/error.log  info;
  8 
  9 #pid        logs/nginx.pid;
 10 
 11 
 12 events {
 13     worker_connections  1024;
 14 }
 15 
 16 
 17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
 20 
 21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 22     #                  '$status $body_bytes_sent "$http_referer" '
 23     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 24 
 25     #access_log  logs/access.log  main;
 26 
 27     sendfile        on;
 28     #tcp_nopush     on;
 29 
 30     #keepalive_timeout  0;
 31     keepalive_timeout  65;
 32 
 33     #gzip  on;
 34 
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
 44             proxy_pass http://www.wzpbk.com:8080/wzpbk/;
 45             proxy_set_header Host $host;
 46         proxy_set_header X-Real-IP $remote_addr;
 47             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 48         }
 49 
 50         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|apk|tar.gz)$ {
 51             expires      30d;
 52         }
 53         location ~ .*\.(js|css)?$ {
 54         proxy_pass http://127.0.0.1:8080;
 55             expires      12h;
 56          }
 57         #error_page  404              /404.html;
 58         # redirect server error pages to the static page /50x.html
 59         #
 60         error_page   500 502 503 504  /50x.html;
 61         location = /50x.html {
 62             root   html;
 63         }
 64 
 65         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 66         #
 67         #location ~ \.php$ {
 68         #    proxy_pass   http://127.0.0.1;
 69         #}
 70 
 71         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 72         #
 73         #location ~ \.php$ {
 74         #    root           html;
 75         #    fastcgi_pass   127.0.0.1:9000;
 76         #    fastcgi_index  index.php;
 77         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 78         #    include        fastcgi_params;
 79         #}
 80 
 81         # deny access to .htaccess files, if Apache's document root
 82         # concurs with nginx's one
 83         #
 84         #location ~ /\.ht {
 85         #    deny  all;
 86         #}
 87     }
 88 
 89 
 90     # another virtual host using mix of IP-, name-, and port-based configuration
 91     #
 92     #server {
 93     #    listen       8000;
 94     #    listen       somename:8080;
 95     #    server_name  somename  alias  another.alias;
 96 
 97     #    location / {
 98     #        root   html;
 99     #        index  index.html index.htm;
100     #    }
101     #}
102 
103 
104     # HTTPS server
105     #
106     #server {
107     #    listen       443 ssl;
108     #    server_name  localhost;
109 
110     #    ssl_certificate      cert.pem;
111     #    ssl_certificate_key  cert.key;
112 
113     #    ssl_session_cache    shared:SSL:1m;
114     #    ssl_session_timeout  5m;
115 
116     #    ssl_ciphers  HIGH:!aNULL:!MD5;
117     #    ssl_prefer_server_ciphers  on;
118 
119     #    location / {
120     #        root   html;
121     #        index  index.html index.htm;
122     #    }
123     #}
124 
125 }

下面是页面代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <style type="text/css">
    body {
    background-image: url(img/saber.jpg);
    }
    </style>
    </head>
  
  <body>
    <h1>网站正在开发中</h1><br>
    <h1>欢迎访问我的博客</h1><br>
    <h1><a href="http://www.cnblogs.com/DanBrown/">DanBrown的博客</a></h1>
  </body>
</html>

不知道为啥看不到图片,请大神教教我怎么配置nginx

DanBrown的主页 DanBrown | 小虾三级 | 园豆:1321
提问于:2017-12-09 14:14
< >
分享
最佳答案
0

首先放入解决问题的链接   http://blog.csdn.net/cxm19881208/article/details/65441865 

下面贴出我修改后的配置文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  wwww.wzpbk.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/apache-tomcat-8.5.23/webapps/wzpbk/;
            index  index.html index.htm index.jsp;                    
            proxy_redirect off;
            proxy_pass http://ip/wzpbk/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        alias /home/apache-tomcat-8.5.23/webapps/wzpbk;
        expires 30d;
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|apk|tar.gz)$ {
        proxy_pass http://ip:8080;
            expires      30d;
        }
        
        location ~ .*\.(js|css)?$ {
        proxy_pass http://ip:8080;
            expires      12h;
         }
         
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

这是个大坑!!!

注意:location通过正则表达式来配置可以访问静态资源,通过正则表达式配置location,里面的proxy_pass一定不能有uri,

即配置成:proxy_pass http://ip:8080/;

或者

proxy_pass http://ip:8080/xxxxx;

都是错的~

 

下面放入我参考的配置文件,

server {

        listen       443;

        server_name  doc.wlyl.com;

        location /yiliao_doc/ {

                proxy_pass http://192.168.9.12:8081;

                proxy_redirect off ;

                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_set_header X-Forwarded-Proto  $scheme;

                proxy_next_upstream error timeout invalid_header;

        }

        location ~ .*\.(html|htm|ico|png|jpg|jpeg|js|css|bmp)$ {

            proxy_pass http://192.168.9.12:8081;

       }


    }
DanBrown | 小虾三级 |园豆:1321 | 2017-12-10 16:18
其他回答(1)
0

nginx中server_name设置的是localhost,应该是www.wzpbk.com

收获园豆:10
dudu | 园豆:30948 (高人七级) | 2017-12-09 14:52

我在线改,加个qq820398513指导一下

支持(0) 反对(0) DanBrown | 园豆:1321 (小虾三级) | 2017-12-09 14:53

改了不好使。

支持(0) 反对(0) DanBrown | 园豆:1321 (小虾三级) | 2017-12-09 15:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册