找到我的nginx实际使用的nginx.conf文件

b.b*_*.b. 110 sysadmin macos nginx

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

Dan*_* Li 215

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


VBa*_*art 31

% ps -o args -C nginx
COMMAND
build/sbin/nginx -c ../test.conf
Run Code Online (Sandbox Code Playgroud)

如果在没有-c选项的情况下运行nginx ,则可以使用该-V选项找出设置为非标准值的configure参数.其中最有趣的是:

--prefix=PATH                      set installation prefix
--sbin-path=PATH                   set nginx binary pathname
--conf-path=PATH                   set nginx.conf pathname
Run Code Online (Sandbox Code Playgroud)

  • 我尝试运行命令,但它们对我不起作用。第二个答案有效 (2认同)

Jin*_* Li 27

双方nginx -tnginx -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 ...
Run Code Online (Sandbox Code Playgroud)

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

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

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


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
Run Code Online (Sandbox Code Playgroud)

这样你就可以通过例如以下方式获得配置文件:

$ ps aux | grep "[c]onf" | awk '{print $(NF)}'
/app/nginx.conf
Run Code Online (Sandbox Code Playgroud)


Cra*_*yne 7

which nginx
Run Code Online (Sandbox Code Playgroud)

将为您提供正在使用的nginx的路径


编辑(2017年1月18日)

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

如果您通过包管理器(如HomeBrew)安装了nginx ...

which nginx
Run Code Online (Sandbox Code Playgroud)

可能不会给你确切的正在使用的nginx的路径.但是你可以找到它

realpath $(which nginx)
Run Code Online (Sandbox Code Playgroud)

正如@Daniel Li所说

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

或者你可以使用这个:

nginx -V
Run Code Online (Sandbox Code Playgroud)

  • `which nginx` 只显示当前用户的 nginx 的 *默认* 路径(甚至不是当前用户 - 当前的 *shell*)。它绝对没有显示“正在使用”nginx 的路径。 (2认同)

Daw*_*žan 7

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

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

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

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