【问题标题】:*44 open() "/usr/share/nginx/html/att-app" failed (2: No such file or directory), 404 not Found*44 open() "/usr/share/nginx/html/att-app" failed (2: No such file or directory), 404 not Found
【发布时间】:2021-09-23 02:32:52
【问题描述】:

我正在尝试通过 nginx 部署 Angular 应用程序。下面是我用来配置 nginx 服务器的 nginx-conf 文件

user  nginx;
worker_processes  auto;
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;
  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;

   root /usr/share/nginx/html;

  server {
    listen 8083;
    
    location /att-app {

       root /usr/share/nginx/html/att-app;
       
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
     }
}

  }
}

这个应用程序必须在另一个应用程序中导入.. url 应该像 http:///att-app。 我可以很好地看到 /usr/share/nginx/html/ 文件夹中的 index.html 和其他 js 文件,但仍然无法访问..

Dockerfile

# ----- BUILD -----
# Base image used for build node js apps
FROM node:12.2.0-alpine AS build
# Set our working directory
WORKDIR /app
# Copy package manifest (we do this in order to cache dependencies)
COPY ./package.json .

# To handle 'not get uid/gid'
RUN npm config set unsafe-perm true

# Install dependencies
RUN npm i
RUN npm install -g @angular/cli@8.3.26
# Copy our application files
COPY . .
# Build a static version of our angular app
RUN ng build --prod --aot --vendor-chunk --common-chunk --delete-output-path
# ----- SERVE -----
# We'll use an nginx base to host our built static assets 
FROM nginx

# Copy our minimal custom configuration
#COPY ./nginx.conf /etc/nginx/nginx.conf

RUN mkdir /usr/share/nginx/html/att-app

RUN chmod -R 777 /usr/share/nginx/html/att-app

# Copy our static assets from our build container
COPY --from=build /app/dist/att-app /usr/share/nginx/html

COPY --from=build /app/dist/att-app /usr/share/nginx/html/att-app

# Copy our minimal custom configuration
#COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf

COPY ./nginx.conf /etc/nginx/nginx.conf

# support running as arbitrary user which belogs to the root group
RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx


EXPOSE 8083

# remove the user directive
# comment user directive as master process is run as user in OpenShift anyhow
RUN sed -i.bak 's/^user/#user/' /etc/nginx/nginx.conf

CMD ["nginx", "-g", "daemon off;"] 

【问题讨论】:

    标签: angular nginx deployment http-status-code-404


    【解决方案1】:

    尝试使用以下配置。这适用于我的 Angular 应用程序。

    server {
        listen       8083;
        root /usr/share/nginx/html;
        index index.html;
    
        location /att-app/ {
            try_files $uri $uri/ /att-app/index.html;
            ...
        }
        ...
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 2015-01-21
      • 2017-02-08
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多