Nginx配置文件详解

# 指定ngnix worker进程运行用户及用户组
#user  nobody;

# ngnix进程数,建议设置为CPU核心数
worker_processes  1;

# 全局错误日志类型 [ debug | info | notice | warn | error | crit ]
#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服务器
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;

    # 开启高效文件传输模式,sendfile指令指定ngnix是否使用sendfile函数来输出文件,对于普通文件设置为on,
    # 如果用来进行下载等磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
    # 注意:如果图片显示不正常把这个设置为off
    sendfile        on;

    # 防止网络阻塞
    #tcp_nopush     on;

    # 长连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 开启gzip压缩输出
    #gzip  on;

	client_max_body_size 2m;

	# 虚拟主机配置
    server {
    	# 监听端口
        listen       80;
        # 域名可以多个,用空格隔开
        server_name  localhost;

        # 默认编码
        #charset koi8-r;

        # 本虚拟主机访问日志
        #access_log  logs/host.access.log  main;

        # 负载均衡
        # upstream test {
        #	# 设置为根据ip的hash结果分配(默认轮询)
        #	iphash;
        #	server localhost:8090 weight=1;
        #	server localhost:8091 weight=1;
        # }
		
		# web前端页面路径
		# root         D:/IDEA/workspace/WEMS/trunk/wems-web;
		
		# location写法规则
		# 1. = 开头表示精准匹配
		#	location = / {
		#		# 精准匹配 / ,后面不能带任何字符串
		#		[ configuration A ]
		#	}
		# 2. ^~ 表示uri以某个常规字符串开头,不是正则匹配
		#	location ^~ /images/ {
		#		# 可以匹配 /images/a.png,/images/b.jpg 等
		#		[ configuration B ]
		#	}
		# 3. ~ 区分大小写正则匹配
		#	location ~ /documents/Abc {
		#		# 匹配到 /documents/Abc 开头的地址,由于优先级原因还要继续往下搜索,其他正则匹配失败才会采用这一条
		#		[ configuration C ]
		#	}
		# 4. ~* 不区分大小写正则匹配
		#	location ~* \.(gif|jpg|jpeg)$ {
		#		# 匹配以.gif, .jpg, .jpeg结尾的地址
		#		[ configuration D ]
		#	}
		# 5. /images/png/ 部分起始路径
		#	location / {
		#		# 匹配 /images/png/ 开始的路径,但由于优先级原因会先被 ^~ /images/ 匹配
		#		[ configuration E ]
		#	}
		# 6. / 通用匹配,如果没有其他匹配,默认匹配
		#	location / {
		#		[ configuration F ]
		#	}
		#
		# 优先级:(location =) > (location 完整路径) > (location ^~) > (location ~,~*) > (location 部分起始路径) > (location /)

		# root与alias的区别
		# root可以用于http、server、location、if,alias只能用于location
		# 
		# location ^~ /t/ {
		# 	root /www/root/html;
		# }
		# 请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。(带上location路径)
		#
		# location ^~ /t/ {
		# 	alias /www/root/html/; # (alias必须用'/'结尾,root可以不用)
		# }
		# 请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/a.html的文件。(替换location路径)

		location /app/ {
			root   html;
			index  index.html index.htm;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;
			proxy_set_header X-NginX-Proxy true;
            # 转发到后端服务地址
			proxy_pass http://localhost:8000/;
			proxy_redirect off;
		}

		# 静态资源配置
		# location ^~ /static/ {
		# 	root D:/Temp/;
		# 	autoindex on; # 开启目录访问,默认为off
		# 	autoindex_exact_size off; # 关闭具体大小,显示KB、MB或GB,默认为on,显示具体大小,单位为byte
		# 	autoindex_localtime on; # 显示服务器的文件修改时间,默认为off,不显示
		# }

		# 静态资源配置gzip优化
		# location ~ *\.(txt|xml|log)$ {
		#	root D:/temp/;
		#	gzip on; # 开启gzip压缩传输
		# 	gzip_min_length 1k; # 低于1KB的资源不压缩
		# 	gzip_comp_level 3; # 压缩级别(1-9),越大压缩率越高,消耗CPU资源越多,建议设置在4左右
		#	gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
		# 	gzip_disable "MSIE[1-6]\."; # 配置禁用gzip条件,支持正则;此处表示ie6及以下不开启gzip
		# 	gzip_vary on; # 是否添加"Vary:Accept-Encoding"响应头
		# 	add_header Content-Type text/plain; # 添加响应头,文件显示为text/plain格式
		# }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


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

}
end
  • 作者:旭仔(联系作者)
  • 发表时间:2022-03-19 22:49
  • 版权声明:自由转载-非商用-非衍生-保持署名
  • 转载声明:如果是转载栈主转载的文章,请附上原文链接
  • 公众号转载:请在文末添加作者公众号二维码(公众号二维码见右边,欢迎关注)
  • 评论