Nginx nginx.conf配置文件详解及内置变量

nginx.conf配置文件详解

#进程守护者:
user  nginx;

#错误日志
error_log  logs/error.log;

#pid进程信息:
pid        logs/nginx.pid;

#worker进程数:(约等于CUP核数,单核就设置1)
worker_processes  1;

#每进程处理连接数:
worker_connections  1024;

#主配置区域结构:
http{
    
    
	#mime文件类型
	include       mime.types;

	#默认为附件类型
	default_type  application/octet-stream;

	#访问日志格式
	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
	                  '$status $body_bytes_sent "$http_referer" '
	                  '"$http_user_agent" "$http_x_forwarded_for"';

	#开启访问日志
	#access_log  logs/access.log  main;

	#延迟发送,优化带宽阻塞
	sendfile        on;
	#tcp_nopush     on;

	#等待超时时间
	#keepalive_timeout  0;
	keepalive_timeout  65;
	
	#网页压缩传输	
	gzip  on;
	
	#第一台http虚拟主机配置
	server{
    
    
		#端口号
		listen       80;

		#网站域名
		server_name  localhost;

		#web字符集
		#charset koi8-r;

		#访问日志
		#access_log  logs/host.access.log  main;

		#匹配192.168.2.1或192.168.2.1/
		location / {
    
    
			#设置网站根目录
		    root   html;

		    #设置默认首页
		    index  index.php index.html index.htm;
		}

		#设置404错误页面
		#error_page  404              /404.html;

		#设置50x错误页面
		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
    
    
		    root   html;
		}

		#访问php文件时直接交给本机apache服务来处理
		location ~ \.php$ {
    
    
		    proxy_pass   http://127.0.0.1;
		}

		#请求php文件时交给php-fpm处理
		location ~ \.php$ {
    
    
		    fastcgi_index  index.php;
		    fastcgi_pass   127.0.0.1:9000;
		    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
		    include        fastcgi_params;
		}

		拒绝所有人访问.htaccess文件
		location ~ /\.ht {
    
    
		    deny  all;
		}
	}

	#第二台http虚拟主机配置
	server {
    
    
	    listen       8000;
	    listen       somename:8080;
	    server_name  somename  alias  another.alias;

	    location / {
    
    
	        root   html;
	        index  index.html index.htm;
	    }
	}

	#配置https虚拟主机
	server {
    
    
	    listen       443 ssl;
	    server_name  localhost;

	    ssl_certificate      cert.pem;
	    ssl_certificate_key  cert.key;

	    ssl_session_cache    shared:SSL:1m;
	    ssl_session_timeout  5m;

	    ssl_ciphers  HIGH:!aNULL:!MD5;
	    ssl_prefer_server_ciphers  on;

	    location / {
    
    
	        root   html;
	        index  index.html index.htm;
	    }
	}
}

Nginx内置变量

$args 请求中的参数(代表的是参数)

location /test {
                echo $args;
        }

发送请求:

curl -d "name=user1&pass=123" -G  http://106.52.36.65/test

剩下的常用内置变量就不做测试了:

$binary_remote_addr        远程地址的二进制表示
$body_bytes_sent           已发送的消息体字节数
$content_length            HTTP请求信息里的"Content-Length"
$content_type              请求信息里的"Content-Type"
$document_root             针对当前请求的根路径设置值
$document_uri              与$uri相同
$host                      请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名 
$http_cookie               cookie 信息 
$http_referer              来源地址
curl -d "name=user1&pass=123" -H "Referer:106.52.36.65/index.php" -G http://106.52.36.65/test

$http_user_agent           客户端代理信息
curl -d "name=user1&pass=123" -H "Referer:106.52.36.65/index.php" -A "php" -G http://106.52.36.65/test

$http_x_forwarded_for      相当于网络访问路径
$limit_rate                对连接速率的限制          
$remote_addr               客户端地址
$remote_port               客户端端口号
$remote_user               客户端用户名,认证用
$request                   用户请求信息
$request_body              用户请求主体
$request_body_file         发往后端的本地文件名称      
$request_filename          当前请求的文件路径名
$request_method            请求的方法,比如"GET"、"POST"等
$request_uri               请求的URI,带参数   
$server_addr               服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址
$server_name               请求到达的服务器名
$server_port               请求到达的服务器端口号
$server_protocol           请求的协议版本,"HTTP/1.0"或"HTTP/1.1"
$uri                       请求的URI,可能和最初的值有不同,比如经过重定向之类的

猜你喜欢

转载自blog.csdn.net/weixin_39218464/article/details/112707207