配置Nginx渲染静态文件并重写文件扩展名或proxy_pass

配置Nginx渲染静态文件并重写文件扩展名或proxy_pass

我已经设置了 Nginx 来处理我的所有静态文件,否​​则将 proxy_pass 到 Node.js 服务器。它工作正常,但我很难重写 URL 以删除 .html 文件扩展名。

upstream my_upstream {
        server 127.0.0.1:8000;
        keepalive 64;
}


server {
        listen       80;
        server_name  staging.mysite.com;
        root /var/www/staging.mysite.org/public;

        access_log /var/logs/staging.mysite.org.access.log;
        error_log /var/logs/staging.mysite.org.error.log;

        location ~ ^/(images/|javascript/|css/|robots.txt|humans.txt|favicon.ico) {

            rewrite (.*)\.html $1 permanent;
            try_files  $uri.html  $uri/ /index.html;

            access_log off;
            expires max;
        }   

        location / {

                proxy_redirect off;
                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_set_header   Host                   $http_host;
                proxy_set_header   X-NginX-Proxy    true;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_cache one;
                proxy_cache_key sfs$request_uri$scheme;
                proxy_pass         http://my_upstream;
        }
}

答案1

我想到了这个。到目前为止,它似乎很有效。

location / {
  try_files $uri $uri/ $uri.html @node;
}
location @node {
  proxy_pass http://127.0.0.1:3000;
}

相关内容