【问题标题】:Nginx 404 or default pageNginx 404 或默认页面
【发布时间】:2017-11-16 02:42:16
【问题描述】:

我在我的 Mac 上开发了一个 Angular 2 应用程序,并使用 Nginx 作为网络服务器在本地运行它。现在我将它部署到 AWS EC2 实例上时遇到了麻烦。这是一个 Linux Red Hat 7 实例。

我已经安装了 Nginx 并从我的 mac 复制了我的工作配置,确保更改 server_name 值和 root,但我得到默认的 nginx 页面或 404 错误。

我在/var/www/dist 设置了我的应用程序,并授予chmod 755 对该路径和dist 中文件的权限。下面是我的配置(注意 server_name 的尖括号不在实际配置中,我只是隐藏了这篇文章的实例名称)。

myhost/index.htmlmyhost/someapppath 的任何尝试都返回404。使用myhost/ 返回默认的nginx 页面。我什至尝试通过创建 html 目录并将文件放在那里进行测试来检查我的根目录,但即使这样也没有被选中。我也在每次配置后重新启动了 nginx,但仍然相同。

有什么想法吗?

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  <hiding instance for this post>.ap-southeast-2.compute.amazonaws.com;
        root /var/www/dist;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            index  index.html index.htm;
            try_files $uri $uri$args $uri/index.html /index.html =404;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
         root   html;
       }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
#include /etc/nginx/conf.d/*.conf;
}

更新

再次修改配置然后查看错误日志后,我现在看到以下内容:

2017/11/16 17:12:14 [crit] 23126#23126: *2 stat() "/var/www/dist/index.html" failed (13: Permission denied)

这更有意义。我已经为 nginx 用户运行了 sudo chown nginx: var/sudo chown -R nginx: /var/www 并重新启动了 nginx,但我仍然被拒绝权限。我是否正确设置了权限?

【问题讨论】:

  • 我很乐意提供帮助!您是否将索引放入/dist?服务器名称也应该是用户将请求的 URL
  • @Colton index.html 位于 /dist 文件夹中,server_name 是我用来访问该站点的公共 DNS 名称。奇怪,这一切都在本地工作。

标签: angular nginx amazon-ec2


【解决方案1】:

已解决,非常棘手的问题。在这篇文章中使用@jsina 的评论解决了:https://stackoverflow.com/a/36389001/2313746

实际上,如果您 mv 来自 /home 的目录,它不会更新权限上下文。

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 2015-01-02
    • 2017-07-18
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2018-08-08
    相关资源
    最近更新 更多