Nginx配置静态文件问题

此为容器访问宿主机tomcat例子

出现问题:通过nginx访问tomcat时无图形界面

 

 

我们访问http://IP:端口/***.png(nginx的访问链接),但是实际访问的静态文件是的容器中的路径:

 

 所以我们应该把宿主机的目录挂载到容器,或者直接拷贝文件到容器内,然后给路径

-v /px/nginx/webapps:/webapps/ROOT

发现nginx访问tomcat时候只能访问首页,点击别的会报404

解决:

在nginx.conf配置里面的server位置下加入如下内容即可:

location ~ .* {
            proxy_pass  http://tomcatserver;  #http://你的upstream配置的名称;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Fonwarded-For $proxy_add_x_forwarded_for;
        }

 

 

 

 

 

 

以下为nginx.conf文件,在docker内为default.conf文件内容(参考)

upstream tomcatserver{
    server 192.168.10.197:8087 weight=1;
        server 192.168.10.197:8089 weight=2;
    }

server {

    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    #location / {
     #   root   /usr/share/nginx/html;
      #  index  index.html index.htm;
   # }
    location ~ \.jsp$ {
        proxy_pass http://tomcatserver;
     }

       location ~ \.(html|js|css|png|gif|ico|jpg)$ {
            root /webapps/ROOT;
        }


    #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   /usr/share/nginx/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;
    #}
}

posted @ 2020-08-19 15:57  px_passion  阅读(158)  评论(0编辑  收藏  举报