簡體   English   中英

NGINX和Angular 2

[英]NGINX and Angular 2

我當前的應用程序用戶路線,如/ 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文件中的內容/

我剛剛遇到同樣的問題並找到了解決方案。 然而,我的基礎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;
        }
    }
}

我在這里也有問題,現在可以在角度文檔中考慮: https//angular.io/guide/deployment

NGinx:使用try_files,如前控制器模式Web應用程序中所述,已修改為index.html服務:

try_files $ uri $ uri / /index.html;

我在HostGator共享托管托管的網站上遇到了同一個子域問題。 它似乎正在運行Apache 2.2.31。

搜索最終導致我“Apache Directives” ,這導致我“自定義錯誤響應”

我能夠通過在子域文件夾中創建一個.htaccess文件來修復我的問題,其中包含以下行:

ErrorDocument 404 /index.html

雖然查看調試工具,但即使成功,我仍然會返回404代碼。

更新:

能夠通過將我的.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。

這就是我的工作配置的樣子,與接受的答案略有不同。 我的項目位於文件夾/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;
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM