使用Nginx和Reac部署到GCP

时间:2019-05-23 15:32:36

标签: nginx google-cloud-platform

我有一个要部署到gcp的项目。我正在使用带有Nginx服务器的create react应用程序。 Nginx conf文件如下所示:

worker_processes auto;

#Provides the configuration file context in which the directives that affect connection processing are specified.
events {
    # Sets the maximum number of simultaneous connections that can be opened by a worker process.
    worker_connections 8000;
    # Tells the worker to accept multiple connections at a time
    multi_accept on;
}

#add_header X-XSS-Protection "1; mode=block";


http {

    # what times to include
    include       /etc/nginx/mime.types;
    # what is the default one
    default_type  application/octet-stream;


    # Sets the path, format, and configuration for a buffered log write
    log_format compression '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $upstream_addr '
    '"$http_referer" "$http_user_agent"';

    server {
        # listen on port 80
        listen 5000;
        # save logs here
        access_log /var/log/nginx/access.log compression;

        server_name frontend.me.com;

        index index.html index.htm index.js;

        root /var/www;
        # where the root here

        # what file to server as index
        index index.html index.htm;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options nosniff;
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm|svg)$ {
            # First attempt to serve request as file, then
            # as directory, then fall back to redirecting to index.html
            root /var/www;
        }


        etag on;
        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 5;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
        gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;

    }
}

Nginx应该只提供index.html和main.js,它们都在var / www文件夹中。我的gcp部署入口是这样的:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: frontend-ip
  labels:
    app: frontendweb
spec:
  rules:
    - host: frontend.me.com
      http:
        paths:
        - backend:
            serviceName: myfrontend
            servicePort: 5000
          path: /

但是,当我部署到gcp时,出现以下错误:无法加载资源:服务器响应状态为404(未找到)main.js.1,这很奇怪,因为当我在本地运行docker容器时,它可以工作精细! 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在nginx配置中将其插入服务器块中,删除重复的索引标识行

        # what file to server as index
        index index.html index.htm;