【问题标题】:Nginx and Amazon EC2 with multiple static subdomains具有多个静态子域的 Nginx 和 Amazon EC2
【发布时间】:2014-10-11 00:19:14
【问题描述】:

目前有一个纯 HTML 网站,其中包含一些嵌入式 JS 代码来控制 HTML 文件之间的流以及图形配置细节。目录如下:html、css、fonts、img、js、sound。在我的本地机器上,我可以轻松打开 HTML 文件并浏览网站。我正在使用带有 nginx 作为反向代理的 Ubuntu Amazon EC2 实例。在实例上运行 express.js 控制的网站时,我通常只需编辑 nginx 配置文件并启动 app/server.js 文件。但是,我不确定如何使用一组具有 CSS 和其他相关资产的静态 HTML 文件来执行此操作。下面是我编写 nginx 文件的尝试,但我不确定我的提议是否可行。

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/website-dashboard/;
        index index.html;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
          default_type "text/html";
          try_files $uri.html $uri $uri/ /index.html;
          access_log off;
        }
}

【问题讨论】:

    标签: html css nginx amazon-ec2


    【解决方案1】:

    使用 NGINX 打开 HTML 文件应该没有问题 - 问题是,考虑到您将 NGINX 作为反向代理运行,那么您将什么作为 Web 服务器运行?

    如果您运行的是 Apache,那么您需要确保它在端口 8080 上运行,并且以下 NGINX 配置应该满足您的要求。

    upstream apachebackend  {
        server 127.0.0.1:8080; #apachebackend
    }
    
    server {
        listen       80 default;
        server_name  www.domain.com domain.com;
        access_log  /var/log/nginx/domain.com.access.log  main;
        error_log  /var/log/nginx/domain.com.error.log;
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    
    ## send request back to apachebackend ##
        location / {
            proxy_pass  http://apachebackend;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            proxy_redirect off;
            proxy_buffering on;
            proxy_buffers 12 12k;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $remote_addr;
        }
    }
    

    如果您将 NGINX 作为 Web 服务器运行,那么您需要进行相应的设置。默认设置就足够了,只需将文件放到默认 vhost 目录中,但我建议使用单独的目录和日志文件正确设置。

    【讨论】:

    • 坦率地说,我不确定默认情况下在 Amazon EC2 实例上安装了什么。有什么办法检查吗?还假设我确实将 apache 作为 Web 服务器运行,您能否简要解释一下为什么我需要将文件复制到“/usr/share/nginx/html”而不是将它们保留在当前目录中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2016-06-16
    • 2017-02-17
    • 1970-01-01
    • 2016-10-15
    • 2021-03-05
    相关资源
    最近更新 更多