首页 文章

NGINX和Angular 2

提问于
浏览
18

我当前的应用程序用户路线,如/ myapp /,/ myapp //,/ myaapp / dept /

我的应用程序当前部署在带有NGINX的内部http服务器中 . 接受外部流量的另一台服务器也运行NGINX并将其转发到内部服务器 .

我根据文档将indexref = / myapp添加到index.html

如果用户转到http://www.myexternalserver.com/myapp,该应用程序可以正常运行 . 如果用户在页面内并单击内部链接(如http://www.myexternalserver.com/myapp/myparameter),则可以正常工作 . 浏览器中的URL更改,页面按预期显示 . 我猜它是由Angular 2处理的 .

不幸的是,当用户直接输入网址时:http://www.myexternalserver.com/myapp/myparameter,我收到NGINX发出的404错误 .

我想我必须配置NGINX设置,但我不知道应该如何修改NGINX的配置或者放在sites-available / default文件中的内容/

4 回答

  • 0

    我刚刚遇到同样的问题并找到了解决方案 . 然而,我的基础href是“/” .

    下面是我的nginx.conf:

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  mysite.com www.mysite.com;
            root /usr/share/nginx/html;
    
            location / {
                try_files $uri$args $uri$args/ /index.html;
            }
        }
    }
    
  • 0

    我在这里也有问题,现在在角度文档中考虑:https://angular.io/guide/deployment

    NGinx:使用try_files,如前控制器模式Web应用程序中所述,经过修改以提供index.html:try_files $ uri $ uri / /index.html;

  • 7

    我在HostGator共享托管托管的网站上遇到了同一个子域问题 . 它似乎正在运行Apache 2.2.31 .

    搜索最终导致我"Apache Directives",这导致我"Custom Error Responses" .

    我能够通过在子域文件夹中创建 .htaccess 文件来解决我的问题,其中包含以下行:

    ErrorDocument 404 /index.html
    

    虽然查看调试工具,但即使成功,我仍然会返回404代码 .

    UPDATE:

    能够通过将 .htaccess 更改为以下内容来解决404问题:

    RewriteEngine On
      # If an existing asset or directory is requested go to it as it is
      RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
      RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
      RewriteRule ^ - [L]
    
      # If the requested resource doesn't exist, use index.html
      RewriteRule ^ /index.html
    

    现在我正确地被重定向到index.html,代码是200 .

  • 41

    这就是我的工作配置的样子,与接受的答案略有不同 . 我的项目位于文件夹 /usr/share/nginx/html/myapp 和index.html基本路径有/ myapp /作为基本URL .

    server {
    
      listen 80;
    
      sendfile on;
    
      default_type application/octet-stream;
    
    
      gzip on;
      gzip_http_version 1.1;
      gzip_disable      "MSIE [1-6]\.";
      gzip_min_length   256;
      gzip_vary         on;
      gzip_proxied      expired no-cache no-store private auth;
      gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      gzip_comp_level   9;
    
    
      root /usr/share/nginx/html;
    
    
      location / {
        try_files $uri$args $uri$args/ $uri/ /myapp/index.html =404;
      }
    
    }
    

相关问题