NGINX

1. 核心概念理解

#location匹配命令
#	~   表示执行一个正则匹配,区分大小写
#	~* 表示执行一个正则匹配,不区分大小写
#	^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
#	=    进行普通字符精确匹配
#	@   定义一个命名的location,使用在内部定向时,例如 error_page, try_files
#	 
#	=前缀的指令严格匹配这个查询。如果找到,停止搜索。
#	所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
#	正则表达式,在配置文件中定义的顺序。
#	如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。
#	 
#	location 匹配的优先级(与location在配置文件中的顺序无关)
#	= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
#	普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
#	^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
#	最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
#	location = / {                     # 只匹配"/".
#	[ configuration A ]
#	}
#	 
#	location / {                        # 匹配任何请求,因为所有请求都是以"/"开始,但是更长字符匹配或者正则表达式匹配会优先匹配
#	[ configuration B ]
#	}
#	 
#	location ^~ /images/ {           # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
#	[ configuration C ]
#	}
#	 
#	location ~* \.(gif|jpg|jpeg)$ {         # 匹配以 gif, jpg, or jpeg结尾的请求. 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
#	[ configuration D ]
#	}

2.配置实际使用




#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    #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;
	
	
	
	
	# 简单文件服务下载功能,本地文件可以下载
	#autoindex on;# 显示目录
	#autoindex_exact_size on;# 显示文件大小
	#autoindex_localtime on;# 显示文件时间
    #sendfile        on;  # 高效传输文件0拷贝
	#server {
	#	charset      utf-8,gbk; 
	#	listen       9050 default_server;
	#	listen       [::]:9050 default_server;
	#	server_name  _;
	#	root         D:\\test\\;
	#	location / {
	#		if ( $request_filename ~* ^.*?\.(html|doc|pdf|zip|docx|txt)$){
	#			add_header Content-Disposition attachment;
	#			add_header Content-Type application/octet-stream;
	#		}
	#	}
	#}
	
	
	# 图片文件预览功能,jpg可以预览,png预览为乱码
    #server {
    #    listen       8088;
    #    server_name  localhost;# 域名为localhost
	#	charset      utf-8,gbk; 
	#	
    #    location / {
    #        root   D:\\test\\nginx-1.18.0\\picture\\;
    #        index  index.html index.htm;
	#		if ( $request_filename ~* ^.*?\.(doc|zip|docx|txt)$){
	#			add_header Content-Disposition attachment;
	#			add_header Content-Type application/octet-stream;
	#		}
    #    }
	#
    #    error_page   500 502 503 504  /50x.html;
    #    location = /50x.html {
    #        root   html;
    #    }
	#
    #}
	
	#HTTP服务器
    #server {
    #    #监听80端口,80端口是知名端口号,用于HTTP协议
    #    listen       80;
    #    #定义使用www.xx.com访问需要hosts配置
    #    server_name  localhost;
	#	#首页
	#	index index.html
	#	#指向webapp的目录
	#	root D:\\workspace\\spring-shiro\\src\\main\\webapp;
	#	#编码格式
	#	charset utf-8;
	#
	#	#代理配置参数
    #    proxy_connect_timeout 180;
    #    proxy_send_timeout 180;
    #    proxy_read_timeout 180;
    #    proxy_set_header Host $host;
    #    proxy_set_header X-Forwarder-For $remote_addr;
	#
    #    #反向代理的路径(和upstream绑定),location 后面设置映射的路径
    #    location / {
    #        proxy_pass http://zp_server1;
    #    }
	#
    #    #静态文件,nginx自己处理,re 匹配开头
    #    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
    #        root D:\\spring-shiro\\src\\main\\webapp\\views;
    #        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
    #        expires 30d;
    #    }
	#
    #    #设定查看Nginx状态的地址
    #    location /NginxStatus {
    #        stub_status           on;
    #        access_log            on;
    #        auth_basic            "NginxStatus";
    #        auth_basic_user_file  conf/htpasswd;
    #    }
	#
    #    #禁止访问白名单设计,ip  
    #    location ~ /api/{
    #        deny 10.11.110.20;
	#		deny 10.11.110.30;
	#		allow 10.1.1.9;
	#		allow 10.8.76.4;
	#		deny all;
    #    }
	#
	#	#错误处理页面(可选择性配置)
	#	#error_page   404              /404.html;
	#	#error_page   500 502 503 504  /50x.html;
    #    #location = /50x.html {
    #    #    root   html;
    #    #}
    #}

	#server {
    #    listen       8088;
    #    server_name  localhost;# 域名为localhost
	#	charset      utf-8,gbk; 
	#	
    #    location / {
    #        root   D:\\test\\nginx-1.18.0\\picture\\;
    #        index  index.html index.htm;
	#		# 下载为文件的配置
	#		if ( $request_filename ~* ^.*?\.(doc|zip|docx|txt)$){
	#			add_header Content-Disposition attachment;
	#			add_header Content-Type application/octet-stream;
	#		}
    #    }
	#
    #    error_page   500 502 503 504  /50x.html;
    #    location = /50x.html {
    #        root   html;
    #    }
	#
    #}


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #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;
    #    }
    #}

}


https://github.com/dunwu/nginx-tutorial

posted @ 2024-08-01 15:53  堕落先锋  阅读(3)  评论(0编辑  收藏  举报