首页 > 解决方案 > 403 禁止 + Nginx + 虚拟主机配置 + NodeJS

问题描述

我在使用 Nginx for NodeJS 应用程序配置服务器虚拟主机时收到 403 Forbidden。

配置文件代码:

server {
  listen 443 default_server;
  listen [::]:443 default_server;

  ssl on;
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  root /mnt/var/www/example.com/app/public/;

  index index.html index.htm index.php;

  server_name example.com www.example.com;

    access_log  /var/log/nginx/dygnostica_access.log;
    error_log   /var/log/nginx/dygnostica_error.log;

  location / {
    include /etc/nginx/proxy_params
    try_files $uri $uri/ =404;
  }
}

server {
   listen 0.0.0.0:80;
   server_name example.com www.example.com;
   rewrite ^ https://$host$request_uri? permanent;
}

我已经根据这篇文章设置了所有配置。

需要帮助解决 403。在此先感谢。

编辑:

错误日志说:

“/mnt/var/www/example.com/app/public/”的目录索引被禁止。

标签: node.jsnginxerror-handlingvirtualhosthttp-status-code-403

解决方案


我认为您对虚拟主机的配置错误。这是我的 nginx 配置文件。我在主域上有标准的 PHP 应用程序(https),在子域上有节点应用程序(在端口 3000 上运行):

#// virtual host for node app:
server {
  server_name node.domain.cz;
  location / {
  proxy_pass http://127.0.0.1:3000;
  }
}

#// host (for PHP app)
server {
        listen 80;
        server_name domain.cz www.domain.cz;
        root /var/www/domain.cz/www;
        index index.php;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }

    listen 443 ssl; # managed by Certbot
    # ssl_certificate ...

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

}

推荐阅读