Nginx 反向代理不传递 CSS 或图像

Nginx 反向代理不传递 CSS 或图像

我有一个 Docker 容器,其中的 Nginx 充当 SSL 反向代理以进行身份​​验证。我有一个 Apache 容器,用于为 Laravel 应用程序提供服务。我总是能得到 html,但没有 css 或图像。在 Chrome 中我得到:

Resource interpreted as Stylesheet but transferred with MIME type text/html:

这是 nginx/sites/ 中的 default.conf

upstream app {
       server 172.18.0.6;
}
server {

listen 443 ssl default_server;
listen [::]:443 ssl default_server ipv6only=on;
server_name rocket.example.com localhost;
root /var/www/public;
index index.php index.html index.htm;
include /etc/nginx/mime.types;
 # tell users to go to SSL version this time
     if ($ssl_protocol = "") {
     rewrite     ^   https://$server_name$request_uri? permanent;
     }

 add_header Strict-Transport-Security "max-age=15768000";
 ssl_certificate /etc/nginx/rocket.crt;
 ssl_certificate_key /etc/nginx/rocket.key;
 ssl_dhparam /etc/nginx/rocket_dhparam.pem;
 ssl_session_cache shared:SSL:10m;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128:AES256:AES:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK";
 error_page 497  https://$host:$server_port$request_uri;
 ssl_verify_client on;
 ssl_verify_depth 10;
 ssl_client_certificate /etc/nginx/allcerts.pem;


location / {
     try_files $uri $uri/ /index.php$is_args$args;
     include /etc/nginx/mime.types;
}

location ~ \.php$ {
        proxy_pass http://app;
        proxy_http_version 1.1;
        proxy_set_header Host $host:$server_port;
        proxy_set_header Referer $http_referer;
        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 https;
        proxy_set_header X-Forwarded-Ssl on;
        proxy_set_header X-Nginx-Proxy true;
        proxy_set_header X-Client-Verify $ssl_client_verify;
        proxy_set_header X-Client-DN $ssl_client_s_dn;
        proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;

        proxy_send_timeout 86400;
        proxy_read_timeout 86400;
        include /etc/nginx/mime.types;


}
    location ~ /\.ht {
    deny all;
    }
}

我在 Google 上找到的解决方案均不起作用。

有人能看出导致这种情况的错误吗?我在两个位置都放置了 proxy_ 指令,效果相同。如能提供任何帮助,我将不胜感激。

答案1

包含 proxy_pass 的位置设置为仅代理 PHP 文件。此处的这一行

location ~ \.php$ {   

我要尝试做的第一件事是将整个块移至主“位置/”块中。

您的 https 重写使用了 if 语句,这并不理想 - 阅读“if 是邪恶的“。如果你想让客户端使用 https,最好的方法是创建一系列服务器来转发,就像这样

# Forward non-www requests to www
server {
  listen       80;
  server_name  example.com www.example.com;
  access_log  /var/log/nginx/hr.access.log main buffer=128k flush=1m if=$log_ua;
  return       301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl http2;
  server_name example.com;

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs//***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

  access_log  /var/log/nginx/hr.access.log main buffer=128k flush=1m if=$log_ua;

  return 301 https://www.example.com$request_uri;
}

相关内容