169

在安装了两个不同版本的 nginx 的客户端服务器上工作。我认为其中一个是用 brew 包管理器(它是一个 osx 盒子)安装的,另一个似乎是用 nginx 打包的 Makefile 编译和安装的。我搜索了服务器上的所有 nginx.conf 文件,但是这些文件都没有定义我在服务器上启动 nginx 时实际使用的参数。我不知道的 nginx.conf 文件在哪里?

4

6 回答 6

294

通过命令行运行nginx -t将发出一个测试并将带有文件路径的输出附加到配置文件(带有错误或成功消息)。

于 2013-11-11T15:38:16.103 回答
50

两者nginx -t都会nginx -V打印出默认的 nginx 配置文件路径。

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ nginx -V
nginx version: nginx/1.11.1
built by gcc 4.9.2 (Debian 4.9.2-10)
built with OpenSSL 1.0.1k 8 Jan 2015
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf ...

如果需要,可以通过以下方式获取配置文件:

$ nginx -V 2>&1 | grep -o '\-\-conf-path=\(.*conf\)' | cut -d '=' -f2
/etc/nginx/nginx.conf

即使您加载了其他一些配置文件,它们仍然会打印出默认值。


ps aux会显示当前加载的 nginx 配置文件。

$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        11  0.0  0.2  31720  2212 ?        Ss   Jul23   0:00 nginx: master process nginx -c /app/nginx.conf

这样您就可以通过例如以下方式实际获取配置文件:

$ ps aux | grep "[c]onf" | awk '{print $(NF)}'
/app/nginx.conf
于 2016-07-24T00:25:59.997 回答
35
% ps -o args -C nginx
COMMAND
build/sbin/nginx -c ../test.conf

如果 nginx 在没有该-c选项的情况下运行,那么您可以使用该-V选项找出设置为非标准值的配置参数。其中对您来说最有趣的是:

--prefix=PATH                      set installation prefix
--sbin-path=PATH                   set nginx binary pathname
--conf-path=PATH                   set nginx.conf pathname
于 2013-11-11T17:02:36.687 回答
10
which nginx

会给你正在使用的 nginx 的路径


编辑(2017 年 1 月 18 日)

感谢 Will Palmer 对此答案的评论,我添加了以下内容......

如果你已经通过 HomeBrew 之类的包管理器安装了 nginx...

which nginx

可能不会给你正在使用的 nginx 的确切路径但是,您可以使用

realpath $(which nginx)

正如@Daniel Li 所提到的

你可以通过他的方法获取nginx的配置

或者你可以使用这个:

nginx -V
于 2016-04-14T21:01:14.477 回答
5

所有其他答案都很有用,但如果没有打开,它们可能对您没有帮助nginxPATH因此您在command not found尝试运行时nginx会得到:

我在 Debian 7 Wheezy 上有 nginx 1.2.1,nginx可执行文件不在PATH,所以我需要先找到它。它已经在运行,所以使用ps aux | grep nginx我发现它位于 上/usr/sbin/nginx,因此我需要运行/usr/sbin/nginx -t.

如果要使用非默认配置文件(即 not /etc/nginx/nginx.conf),请使用参数运行它-c/usr/sbin/nginx -c <path-to-configuration> -t.

您可能还需要将其作为 运行root,否则 nginx 可能无权打开例如日志,因此该命令将失败。

于 2019-02-15T18:05:55.320 回答
1

除了@Daniel Li 的回答,使用 Valet 安装 nginx 也将使用 Velet 配置,这可以在“/usr/local/etc/nginx/valet/valet.conf”中找到。nginx.conf 文件将导入此 Valet conf 文件。您需要的设置可能在 Valet 文件中。

于 2017-10-24T12:29:57.733 回答