[英]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.