【问题标题】:Vue deploy with Apache: routes don't work in history modeVue 使用 Apache 部署:路由在历史模式下不起作用
【发布时间】:2018-09-13 08:48:04
【问题描述】:

我在使用 Apache(HTTPs 也是)部署 vue 应用程序(没有数据库或服务器端代码)时遇到问题。这是我的 .conf 文件:

<VirtualHost *:80>
ServerAdmin myemail@gmail.com
ServerName mydomain.me
ServerAlias www.mydomain.me
DocumentRoot /var/www/mydomain.me/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/mydomain.me/dist>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.me [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.me
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

当我访问 mydomain 时,首页已正确加载(使用 npm run build 构建的 index.html),但是所有路由都不起作用(即 /portfolio)。

但是,如果我将路由器模式从 history 切换到 hash,一切正常 (/#/portfolio),但我想保持 history 模式处于活动状态。

提前致谢!

【问题讨论】:

    标签: apache vue.js vue-router


    【解决方案1】:

    最终找到了一个解决方案 - 不确定它是否是最好的,但它适用于我的情况。

    /etc/apache2/sites-enabled/mydomain.me-le-ssl.conf

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName mydomain.me
        ServerAlias www.mydomain.me
        DocumentRoot /var/www/mydomain.me/dist
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        <Directory /var/www/mydomain.me/dist>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =mydomain.me [OR]
        RewriteCond %{SERVER_NAME} =www.mydomain.me
        RewriteCond %{HTTPS} !=on
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/mydomain.me/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.me/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
    </IfModule>
    

    .htaccess on /var/www/mydomain.me/dist

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.*) index.html [QSA,L]
    </IfModule>
    

    【讨论】:

    • 这里的重要部分是您必须从您的 .conf 文件的 ssl.conf 版本中启用 AllowOverride。如果您只在sites-available/mydomain.me.conf 中启用了它,但您强制使用HTTPS,它不会生效。
    【解决方案2】:

    您的情况很棘手,因为那些 RewriteConds 和 RewriteRule 您似乎正在重定向到 HTTPS。

    如果是这样,这里有一些可行的方法:

    /etc/apache2/sites-enabled/mydomain.me-le-ssl.conf

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName mydomain.me
        ServerAlias www.mydomain.me
        DocumentRoot /var/www/mydomain.me/dist
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        <Directory /var/www/mydomain.me/dist>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =mydomain.me [OR]
        RewriteCond %{SERVER_NAME} =www.mydomain.me
        RewriteCond %{HTTPS} !=on
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/mydomain.me/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.me/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
    </IfModule>
    

    .htaccess on /var/www/mydomain.me/dist

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.html [L]
    </IfModule>
    

    【讨论】:

    • 是的,我使用的是 Let's Encrypt 证书,因此 HTTPs 是强制性的。当我尝试该配置时,我在 Apache 重新启动时收到以下错误日志: apa​​che2[3848]: AH00526: Syntax error on line 21 of /etc/apache2/sites-enabled/mydomain.me.conf apache2[3848]: RewriteBase:仅在每个目录的配置文件中有效 apache2[3848]:Apache 错误日志可能有更多信息。 systemd[1]: apache2.service: 控制进程退出,code=exited status=1 sudo[3842]: pam_unix(sudo:session): 用户 root 会话关闭 systemd[1]: 无法启动 LSB: Apache2 web server
    • 这似乎有效,但在清除我的缓存后它没有 :( 发生的事情是我得到一个“未捕获的语法错误:意外的令牌 prntscr.com/j0swej)。知道为什么吗?
    • 那个文件app...js文件的完整路径是什么?
    • 我所有的 JS 文件都指向 /static/js/[file.js]。如果我尝试在单独的选项卡中打开它们,我会得到一个空白结果。顺便说一下,这是我当前的完整配置文件 - prntscr.com/j0y5jj。我(错误地)更新了我的 HTTP conf 文件,而我需要工作的是 HTTPs(443)。然而,无法让 JS 文件使用它。
    • 我的意思是我需要在浏览器的地址栏中查看您正在尝试的 app.js 文件的整个路径。无论如何,我已经添加了我认为可能是对您告诉您正在使用的版本的改进;试试看。
    猜你喜欢
    • 2018-02-16
    • 2021-06-03
    • 2016-07-24
    • 2017-06-15
    • 2018-07-07
    • 2020-01-19
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多