【问题标题】:Trouble running two web applications on the same domain on nginx在 nginx 上的同一域上运行两个 Web 应用程序时出现问题
【发布时间】:2019-01-20 07:58:07
【问题描述】:

我想要一个 nginx 服务器在同一个域上托管 Web 应用程序,但路径不同。 例如, http://example.org/booksapp/signin.html 应该指向第一个应用程序, http://example.org/shoesapp/signin.html 应该指向第二个应用程序

在我的主机中,我有两个文件夹,每个应用一个文件夹: /var/webfolder/booksapp 和 /var/webfolder/shoesapp

我的nginx配置如下

server {
    server_name example.org;

    index index.html index.htm;

    location /foodapp {
        root /var/webfolder;
        try_files $uri $uri/ =404;
    }
    location /shoesapp {
        root /var/webfolder;
        try_files $uri $uri/ =404;
    }

    listen [::]:80;
    listen 80;
}

此配置不起作用。我的浏览器在尝试加载任一 Web 应用程序时只显示一个空白页面。 同时,nginx 日志文件显示应用程序尝试加载的每个资源的 404 列表。

我做错了什么?

【问题讨论】:

  • 这些是什么类型的网络应用程序?静态网站?
  • 没有; HTML 和 JS。

标签: nginx nginx-location nginx-config


【解决方案1】:

您必须更改您的 html 文件以使用正确的路径。

您的“booksapp”位于example.org/booksap/,html 页面必须使用相同的路径加载任何静态资源。

这是一个示例 html 文件的头部部分。如果你将它部署到你的任何站点,它将无法工作,除非你指定正确的路径,否则 nginx 找不到“normalize.css”和“styles.css”。现在 nginx 充当两个应用程序之间的路由器,你不能只要求一个文件,你必须指定哪个应用程序。

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type">
  <title>sakura demo</title>
  <link href="normalize.css" rel="stylesheet" type="text/css">
  <link href="styles.css" rel="stylesheet" type="text/css" media="all">
</head>

应该是:

  <link href="booksap/normalize.css" rel="stylesheet" type="text/css">
  <link href="booksap/styles.css" rel="stylesheet" type="text/css" media="all">

【讨论】:

  • 这与我的问题有关。我有一个 导致在根目录中搜索所有资源(顺便重定向到 /usr/share/nginx/html)
猜你喜欢
  • 2023-03-25
  • 2014-07-21
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
相关资源
最近更新 更多