简体   繁体   English

Nginx安全提供静态文件

[英]Nginx secure serve static file

im buildin a MEAN stack application, and i just found out that it's a best practice to let Nginx serve static file (Currently my node is serving static file) and use reverse proxy. 我在一个MEAN堆栈应用程序中进行了构建,我发现这是让Nginx提供静态文件(当前,我的节点提供静态文件)并使用反向代理的最佳实践。 so i was able to serve a static file and reverse proxy on Nginx, my question is, is there a way to secure the access to the static file? 所以我能够在Nginx上提供静态文件和反向代理,我的问题是,有没有办法保护对静态文件的访问?

This is my Nginx code 这是我的Nginx代码

server {
    listen 80;


    location /static {
    alias /var/www/project/public;
    autoindex off;
} 

    location / {
        proxy_pass http://127.0.0.1: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;
    }
}

Under the public folder, i have style.css so when i go to the url localhost/static/style.css , i could see my code, so i imagine lets say i deployed my website to the public and have it's domain name, the users could access my static files by just going to www.domainname.com/static/style.css Is this normal? 在公共文件夹下,我有style.css,所以当我进入url localhost/static/style.css ,我可以看到我的代码,因此我可以说我将我的网站部署到了公众那里并拥有它的域名,用户只需访问www.domainname.com/static/style.css即可访问我的静态文件,这正常吗? or there's a way to just limit the access to NodeJS server? 还是有一种方法可以限制对NodeJS服务器的访问? being the only thing could access the static file? 是唯一可以访问静态文件的东西? or im getting this wrong. 或即时通讯搞错了。

Thanks! 谢谢! sorry im new to this web development world, but im learning. 对不起,我是这个Web开发领域的新手,但我正在学习。

You can limit access using nginx by adding the following to your location definition: 您可以通过在位置定义中添加以下内容来使用nginx限制访问:

#This would be the IP of the server you want to have access to your protected file
allow 123.123.123.123/32;
deny all;

But in this case, you don't want to restrict access to your static files. 但是在这种情况下,您不想限制对静态文件的访问。 The user loading the web page needs access to the css files to display it correctly. 加载网页的用户需要访问css文件才能正确显示它。 If you were to watch the network traffic of when you loaded a web page, you would see that your browser downloads all the client side CSS, JS, and HTML files it needs to run properly. 如果要查看加载网页时的网络流量,您会看到浏览器下载了正常运行所需的所有客户端CSS,JS和HTML文件。 So it is completely normal for people to be able to just look at CSS files that are hosted statically. 因此人们只看静态托管的CSS文件是完全正常的。 Usually a backend NodeJS server has no use for CSS files. 通常,后端NodeJS服务器不使用CSS文件。

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

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