【问题标题】:Where to put nginx configuration file?nginx配置文件放在哪里?
【发布时间】:2017-11-19 20:45:01
【问题描述】:

我知道我应该放置这段代码以使 HTML5 历史回退:

location / {
  try_files $uri $uri/ /index.html;
}

https://router.vuejs.org/en/essentials/history-mode.html

但是到哪个文件?试过google,没用,把上面的代码放在/etc/nginx/nginx.conf里会让nginx不工作。

我在 laravel 中使用 vagrant Homestead。

请帮忙。

【问题讨论】:

  • 您能具体说明您所面临的具体错误是什么吗?
  • 是的,该配置应该进入/etc/nginx/nginx.conf.
  • 我将该代码放在 /etc/nginx/nginx.conf 中,当我“sudo service nginx restart”时,错误是“nginx.service 的作业失败,因为控制进程以错误代码退出。有关详细信息,请参阅“systemctl status nginx.service”和“journalctl -xe”。'
  • 运行这个命令less /var/log/nginx/error.log 看看有什么错误。
  • 这里是错误 > 2017/06/17 01:24:59 [emerg] 1340#1340: "location" 指令在 /etc/nginx/nginx.conf:87 中是不允许的 > 2017/06/17 01:27:33 [emerg] 1369#1369:“位置”指令在 /etc/nginx/nginx.conf:87 > 2017/06/17 01:44:16 [emerg] 中不允许2851#2851:“位置”指令在 /etc/nginx/nginx.conf:66 中不允许使用

标签: laravel nginx vagrant vuejs2 html5-history


【解决方案1】:

这是nginx.conf 文件的条目,位于

/etc/nginx/nginx.conf

将此添加到配置中:

location / {
  try_files $uri /index.html;
}

【讨论】:

  • 这是我在 /etc/nginx/nginx.conf > 2017/06/17 01:24:59 [emerg] 1340#1340: "location" 指令中提到的代码时出现的错误在 /etc/nginx/nginx.conf:87 > 2017/06/17 01:27:33 [emerg] 1369#1369: 在 /etc/nginx/nginx.conf 中不允许使用“位置”指令:87 > 2017/06/17 01:44:16 [emerg] 2851#2851: /etc/nginx/nginx.conf:66 中不允许使用“位置”指令
  • 我相信在运行宅基地时会进入 http { } 部分
  • 终于没有错误了,但是html5历史回退还是不行
  • 查看我的答案的编辑,看来你不需要$uri/
【解决方案2】:

将配置放入/etc/nginx/nginx.conf

server { 
    location / {
      try_files $uri $uri/ /index.html;
    } 
}

【讨论】:

  • 试过了,这里是错误信息:>2017/06/17 01:55:02 [emerg] 3408#3408: "server" directive is not allowed here in /etc/nginx/nginx .conf:65 >2017/06/17 01:55:06 [emerg] 3420#3420: /etc/nginx/nginx.conf:65 中不允许使用“server”指令
  • 只要确保server {} 低于events {} 高于http {}
  • @Ganesh,events{} 和 http{} 必须运行吗?
【解决方案3】:

最后,我让 nginx 使用 html5 后备。

打开 /etc/nginx/site-available/homestead.app,或您在 homestead.yaml 文件中指定的任何域。

将“位置”部分替换为

    location / {
      try_files $uri $uri/ /index.php;
    }

然后保存并打开 laravel web.php(路由器)。把这段代码:

Route::get('/{vue?}/{vue2?}/{vue3?}', function () {
//    return view('welcome');
    if ( ! request()->ajax() ) {
        return view('index');
    }
});

以上代码阻止 laravel 返回错误 404。现在 vue html5 历史回退可以在没有 # 符号的情况下工作。

感谢大家的帮助,如果没有你们,我可能无法解决这个问题。

【讨论】:

    【解决方案4】:

    如果您正在寻找 NGINX 配置文件的位置:

    • 主配置文件为/etc/nginx/nginx.conf
    • 此文件的默认版本将所有匹配/etc/nginx/conf.d/*.conf 的文件加载到其http 块中。
    • 所以如果你想指定一个server 块,你可以覆盖/etc/nginx/conf.d/default.conf 或者在那里添加你自己的.conf 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      相关资源
      最近更新 更多