简体   繁体   English

静态内容Node.js应用反向代理的NgInx禁止错误

[英]NgInx forbidden error for static content nodejs app reverse proxy

I am using NgInx reverse proxy for my expressjs website with HTTPS. 我正在通过HTTPS为我的expressjs网站使用NgInx反向代理。 website is getting accessed with https without any issue and requests are getting at Nodejs server but problem is with Static content. 使用https可以正常访问网站,并且请求正在Nodejs服务器上,但是静态内容存在问题。

All static contents(JS, CSS, Images, fonts etc.) are throwing 403 forbidden error . 所有静态内容(JS,CSS,图像,字体等)都抛出403禁止错误

Below is my ngInx configuration file content: 以下是我的ngInx配置文件的内容:

server {
    listen  80;
    listen 443 ssl;
    ssl on;
    ssl_certificate /home/ec2-user/certs/myapp.bundle.crt; 
    ssl_certificate_key /home/ec2-user/certs/myapp.key;

    root /home/ec2-user/myapp;

    server_name example.com;
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;

    location / {
       #try_files $uri $uri/ /index.php?$args ;
       #try_files $uri $uri/ =404;
        proxy_pass https://example.com:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

I am using amazon EC2 and CentOS. 我正在使用Amazon EC2和CentOS。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

location / {
        proxy_pass https://example.com:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

You are redirecting all traffic to to nodejs. 您正在将所有流量重定向到nodejs。 Thats means expressjs will serve static content. 那就意味着expressjs将提供静态内容。

To serve static files, use the express.static built-in middleware function in Express. 要提供静态文件,请使用Express中内置的express.static中间件功能。

Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly. 将包含静态资产的目录的名称传递给express.static中间件函数,以开始直接提供文件。 For example, use the following code to serve static files in a directory named public: 例如,使用以下代码在名为public的目录中提供静态文件:

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

More info here: https://expressjs.com/en/starter/static-files.html 此处提供更多信息: https : //expressjs.com/en/starter/static-files.html

You are trying to server static files from your NodeJS app which is not right approach since you are using Nginx. 您正在尝试从NodeJS应用程序中处理静态文件,这是不正确的方法,因为您正在使用Nginx。 Add a below kind of block to you config 将以下种类的块添加到您的配置中

location ~ \.(css|gif|png|jpe?g|ico|js|swf|ttf|woff) {
    expires 30d;
    root /home/ec2-user/myapp;
    try_files $uri =404;
}

You might need to adjust the root folder based on how you are using your assets. 您可能需要根据资产的使用方式调整根文件夹。 But if you are using it same as /home/ec2-user/myapp then you can skip adding this line 但是,如果您使用的方法与/home/ec2-user/myapp则可以跳过添加此行的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM