为什么我不能在apache中阻止单个node.js文件?

Sal*_*ali 6 apache node.js

我有一个apache服务器,除了我的应用程序,我有一个node.js websocket应用程序.问题是任何人都可以通过在URL中导航到文件内容来阅读文件内容.我试图阻止直接访问其中一个文件(我已经设法阻止node.js文件夹).

我正在修改我的配置文件:apache2/apache2.conf.假设我的文件在/var/www/server/node_start.js我试图遵循:

<Files /var/www/server/node_start.js>
        Order allow,deny
        Deny from all
</Files>

<FilesMatch /var/www/server/node_start.js>
        Order allow,deny
        Deny from all
</FilesMatch>

<Files /server/node_start.js>
        Order allow,deny
        Deny from all
</Files>

<FilesMatch /server/node_start.js>
        Order allow,deny
        Deny from all
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)

这些都没有成功.我看过其他帖子,看起来我和其他人一样.知道我失败的原因吗?

PS我无法阻止整个目录,因为还有很多其他文件不应该被阻止.

raj*_*ade 4

您使用了错误的方法来使用 node.js 和 apache 服务器。使用 Node.js 的方法如下:

  1. Node.js 提供了服务器和客户端。因此,您需要创建服务器来运行node.js
  2. 我使用express在node.js中使用端口创建服务器。如果您打算使用express,那么不要忘记添加app.enable('trust proxy');app.js
  3. 创建服务器后,它需要以 node.js.Ex 开头:-node sever.jsnode app.js
  4. 您可以使用以下方式访问节点服务器http://localhost:{port}/

  5. 您可以使用forever或nodemon来运行节点服务器。有关更多信息,请查看链接NodemonForever

  6. 您可以将应用程序部署在任何路径,包括 www. 如果您将应用程序放在 www 目录之外。

  7. 确保 node.js 应用程序目录必须具有 apache 或 ngnix 的适当所有权和权限。在授予所有权之前,请检查 apache 或 ngnix 用户的名称。

  8. 对于用户所有权例如:chown -R www:data www:data {/path_to_node_applicatoin}

  9. 对于写入权限例如:chmod -R 775 {/path_to_node_applicatoin}

  10. 启动服务器后,您需要在 apache 和 nginx 服务器中使用代理来全局访问您的站点。

  11. 如果您计划使用 node.js 使用 websocket,则需要 http 版本 1.1 .Ex: proxy_http_version 1.1;;
  12. 配置apache服务器以支持node.js服务器如下:

    <VirtualHost *:80>
            ServerAdmin nodeadmin@example.com
            ServerName example.com
            ServerAlias www.example.com
    
            ProxyRequests off
    
            <Proxy *>
                Order deny,allow
                Allow from all
            </Proxy>
    
            <Location />
                ProxyPass http://127.0.0.1:3000/ #use the port which you specified for node application.
                ProxyPassReverse http://127.0.0.1:3000/
            </Location>
        </VirtualHost>
    
    Run Code Online (Sandbox Code Playgroud)
  13. 配置ngnix支持node.js如下:

         server {
               listen 80;
               server_name example.com;
               root /var/www/stack/nodejsapp;
               index index.html index.htm;
                location / {
                      rewrite ^/socket/(.*) /$1 break;
                      proxy_pass http://127.0.0.1:3000; #use the port which you specified for node application.
                      proxy_redirect off;
                      proxy_set_header X-Real-IP $remote_addr;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-NginX-Proxy true;
                      proxy_set_header X-Forwarded-Host $host;
                      proxy_set_header X-Forwarded-Server $host;
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                      proxy_http_version 1.1;
                      proxy_set_header Upgrade $http_upgrade;
                      proxy_set_header Connection "upgrade";
                      proxy_set_header Host $host;
                }
           }
    
    Run Code Online (Sandbox Code Playgroud)

注意:确保您已在 apache 和 ngnix 中启用代理支持。