在Nginx位置路径下提供Angular应用

时间:2019-11-12 10:24:45

标签: angular nginx

我正在尝试在Nginx服务器中提供Angular应用(dist)。我尝试了很多在Web上找到的配置,并且应该可以使用,但是不能。

当我转到my-site.com/app时,该应用程序正在运行,但是导航后刷新页面时,该应用程序无法运行。

my-site.com/app没关系 my-site.com/app/dashboard 404 my-site.com/app/* 404

因此,my-site.com/app/*应该始终为/var/www/angular-app/dist/index.html服务。

这是我添加到Nginx服务器conf中的配置块:

location /app {
    index index.html;
    alias  /var/www/angular-app/dist;
    try_files $uri $uri/ index.html =404;
    add_header Cache-Control "no-store, no-cache, must-revalidate";
}

这是完整的443块,也许rootalias路径有问题。此配置文件属于Symphony 1.4应用程序。

server {

    listen 443;
    client_max_body_size 50M;
    ssl on;
    ssl_certificate        /etc/nginx/certs/server.crt;
    ssl_certificate_key    /etc/nginx/certs/server.key;

    set $website_host "stage.my-site.com";
    set $website_root "/var/www/mainproject/web";
    set $default_controller "frontend_stage.php";
    set $symfony_root "/var/www/mainproject/lib/vendor/symfony";

    server_name $website_host;

    root $website_root;

    index $default_controller;

    charset utf-8;

    location /sf/ {
        # path to folder where all symfony assets are located
        root $symfony_root/data/web/;
        expires max;
    }

    location ~ \.php($|/) {

        set $script $uri;
        set $path_info "";

        if ($uri ~ "^(.+\.php)(/.*)") {
            set $script $1;
            set $path_info $2;
        }

        include /etc/nginx/fastcgi_params;

        #fastcgi_pass backend;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $website_root$script;
        fastcgi_param SCRIPT_NAME $script;
        fastcgi_param PATH_INFO $path_info;
        break;
    }

    location / {

        if (-f $request_filename) {
            expires max;
            break;
        }

        try_files $uri @rewrite;
    }

    location /app {
        index index.html;
        alias  /var/www/angular-app/dist;
        try_files $uri $uri/ index.html =404;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }

    location @rewrite {
        rewrite ^(.*)$ /frontend_stage.php$1 last;
    }

    location /api/ {
      try_files $uri /api_stage.php?$query_string;
    }
}

0 个答案:

没有答案