【问题标题】:NGINX Config for Express Static Files用于 Express 静态文件的 NGINX 配置
【发布时间】:2019-05-07 11:37:30
【问题描述】:

我正在运行 nginx,并且我还有一个带有反应前端的快速后端服务器。我遇到的问题是来自 express 的静态文件。例如,我有一些带有调用 css/style.css 和 js 目录的标题的车把视图文件,这些文件目前在 Chrome 中有效,但在 IE、Edge 或 Safari 中无效。在这些浏览器中,控制台显示 404,并且样式当然不适用。

我从我的车把视图页面调用 style.css,如下所示:

<link rel="stylesheet" href="css/style.css">

应该这样设置,如果我要访问http://sitename.com/css/style.css,我会从 /var/www/sitename.com/html/node/public/css/style.css 位置看到 style.css。这实际上似乎在 Chrome 中有效,但在其他浏览器中无效。

我的快递应用中有此声明

app.use(express.static('public'));

我有一个这样的目录结构:

/var/www/sitename.com/html/node (node express app is running from here)
/var/www/sitename.com/html/node/public (public folder for static files from express)
                                   -> css (folder)
                                   -> js (folder)

我的 nginx 设置如下:

server {
  listen 80;
  server_name _;
  root /var/www/sitename.com/html;
  index index.php index.html;
  server_name sitename.com www.sitename.com;

  location /phpmyadmin {
    try_files $uri $uri/ =404;
  }

  location / {
    root /var/www/sitename.com/html/node/public/;
    try_files $uri @backend;
  }

  location @backend {
    proxy_pass http://localhost:42134; 
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
  }

  # set max upload size
  client_max_body_size 2G;
  fastcgi_buffers 64 4K;

  access_log /var/log/nginx/http_access.log combined;
  error_log /var/log/nginx/http_error.log;

  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  } 

  location ~ \.php$
  {
    try_files      $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }

  location ~* \.(htaccess|htpasswd) {
    deny all;
  }

  # set long EXPIRES header on static assets
  location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
    expires 30d;
    access_log off;
  }
}

【问题讨论】:

  • 奇怪,因为您通过 nginx 提供公共文件,所以您应该在 app.js 中注释 app.use(express.static('public'));。您也应该尝试在@backend 服务器中设置索引指令(我认为)...
  • 在我的 app.js 中注释掉 express.static 行不会导致任何变化。我还测试了在@backend 下添加索引指令,似乎没有任何明显的差异。

标签: node.js express nginx nginx-location


【解决方案1】:

解决方案最终是删除 app.js 中的 express.static 行,然后在 nginx 配置中我不得不重新排列:

    server {
    listen 80;
server_name _;

    root /var/www/sitename.com/html;
    index index.php index.html;

    server_name sitename.com www.sitename.com;

    location /phpmyadmin {
            root /var/www/sitename.com/html;
            try_files $uri $uri/ =404;
    }

location /dashboard {
            index index.php index.html;
            try_files $uri @backend;
            }

location /static {
            root /var/www/sitename.com/html;
            try_files $uri $uri/ =404;
            }



    location / {
            index index.php index.html;
            try_files $uri @backend;
            }

    location @backend {
            proxy_pass http://localhost:42134; 
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # Following is necessary for Websocket support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

}


    # set max upload size
    client_max_body_size 2G;
    fastcgi_buffers 64 4K;

    access_log /var/log/nginx/http_access.log combined;
    error_log /var/log/nginx/http_error.log;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }   

    location ~ \.php$
    {
        try_files      $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* \.(htaccess|htpasswd) {
        deny all;
    }

    # set long EXPIRES header on static assets
    location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
        expires 30d;
        access_log off;
    }

}

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多