.htaccess – nginx中MEAN-stack应用程序的漂亮url

前端之家收集整理的这篇文章主要介绍了.htaccess – nginx中MEAN-stack应用程序的漂亮url前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

(*正如一些评论建议,我删除了index.ejs并在项目中使用index.html遵循this answer.所以我调整了我的OP *)

我在Mac上用apache开发了一个可以请求的MEAN-stack应用程序
https://开头本地主机:3000 /#/家.在使用Nginx服务器进行生产时,可以请求应用程序
 https://www.myapp.io/#/home.由于角度ui-router,在所有情况下都需要片段标识符#.

所以我想在没有#(例如,https://www.myapp.io/home,https:// localhost:3000 / home)的情况下制作漂亮的网址.我做了以下事情:

1)添加$locationProvider.html5Mode(true); app.config中的$locationProvider.hashPrefix(”)([‘$stateProvider’….

2)添加< base href =“/”/>在index.html中

因此,https:// localhost:3000 /#/ home会自动更改为浏览器栏中的https:// localhost:3000 / home,类似于https://www.myapp.io/#/home.

但是,在浏览器中直接输入https:// localhost:3000 / home或https://www.myapp.io/home会引发错误(我不知道如何转换前一个< h1><%=消息%>< / h1>< h2><%= error.status%>< / h2>< pre><%= error.stack%>< / pre>错误. ejs到error.html,所以我没有更多细节).

所以现在,目标是使https:// localhost:3000 / home和https://www.myapp.io/home工作.

按照this thread,我在app.js中添加了以下内容

  1. app.use('/js',express.static(__dirname + '/js'));
  2. app.use('/dist',express.static(__dirname + '/../dist'));
  3. app.use('/css',express.static(__dirname + '/css'));
  4. app.use('/partials',express.static(__dirname + '/partials'));
  5. app.all('/*',function(req,res,next) {
  6. res.sendFile('index.html',{ root: __dirname });
  7. });

在mac的apache中,这是我的httpd-vhosts.conf,重启apache之后,
https:// localhost:3000 / home仍然返回错误.

在生产中,这是Nginx服务器块.重新启动Nginx后,https://www.myapp.io/home仍然会返回错误.

  1. server {
  2. listen 443 ssl;
  3. server_name myapp.io www.myapp.io;
  4. ssl_certificate /etc/letsencrypt/live/myapp.io/fullchain.pem;
  5. ssl_certificate_key /etc/letsencrypt/live/myapp.io/privkey.pem;
  6. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  7. ssl_prefer_server_ciphers on;
  8. ssl_dhparam /etc/ssl/certs/dhparam.pem;
  9. ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:EC$
  10. ssl_session_timeout 1d;
  11. ssl_stapling on;
  12. ssl_stapling_verify on;
  13. add_header Strict-Transport-Security max-age=15768000;
  14. index index.html;
  15. root /opt/myapp
  16. location / {
  17. try_files $uri $uri/ /index.html;
  18. }
  19. location ~ /.well-known {
  20. allow all;
  21. }
  22. location / {
  23. proxy_set_header Host $host;
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. proxy_set_header X-Forwarded-Proto $scheme;
  26. proxy_set_header Accept-Encoding "";
  27. proxy_set_header Proxy "";
  28. proxy_pass https://127.0.0.1:3000;
  29. # These three lines added as per https://github.com/socketio/socket.io/issues/1942 to remove sock$
  30. proxy_http_version 1.1;
  31. proxy_set_header Upgrade $http_upgrade;
  32. proxy_set_header Connection "upgrade";
  33. }
  34. }

有人可以帮忙吗?

最佳答案
我认为你应该问自己更好的问题是为什么你没有index.html?也许我已经睡了太久了,但是浏览器实际上不能在网页上自己执行JavaScript,没有来自HTML文档的底层DOM.

一旦你理解了这一点,你可能会成功获得GitHub上angular-ui的官方配置建议.

猜你在找的Nginx相关文章