debian

Debian Nginx 配置文件中的变量

小樊
60
2025-04-21 04:13:07
栏目: 智能运维

在 Debian 系统中,Nginx 配置文件通常位于 /etc/nginx/nginx.conf。在这个配置文件中,你可以使用变量来简化配置和提高灵活性。以下是一些常用的 Nginx 变量:

  1. $scheme:当前请求的协议(http 或 https)。
  2. $host:当前请求的主机名(包括端口号,如果有的话)。
  3. $server_name:当前请求的服务器名称。
  4. $request_uri:当前请求的 URI,不包括协议、主机名和端口。
  5. $document_root:当前请求的文档根目录。
  6. $fastcgi_script_name:FastCGI 脚本的路径。
  7. $fastcgi_param:传递给 FastCGI 脚本的参数。
  8. $http_user_agent:客户端 User-Agent 头部。
  9. $http_cookie:客户端 Cookie 头部。
  10. $remote_addr:客户端的 IP 地址。
  11. $remote_port:客户端的端口号。
  12. $server_port:服务器的端口号。

以下是一个简单的 Nginx 配置文件示例,演示了如何使用这些变量:

http {
    server {
        listen 80;
        server_name example.com www.example.com;

        root /var/www/html;
        index index.html index.htm;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
}

在这个示例中,我们使用了 $host 变量来设置 server_name,使用 $document_root 变量来设置文档根目录,以及使用 $fastcgi_script_name$fastcgi_param 变量来配置 FastCGI 脚本参数。

你可以根据需要使用这些变量来定制你的 Nginx 配置。更多关于 Nginx 变量的信息,你可以查阅官方文档:http://nginx.org/en/docs/variables.html

0
看了该问题的人还看了