1、Nginx的配置

一、基本配置
1、进入配置文件目录
vim /usr/local/nginx/conf/nginx.conf

2、修改端口参数
	http-->server-->listen	
	
3、修改服务名参数
	http-->server-->server_name
	
4、设置服务编码
	http-->server中增加:charset utf-8;
	
5、如需多个端口服务可在http下配置多个server

6、重新载入配置文件
	/usr/local/nginx/sbin/nginx -s reload

二、配置错误页面
以下配置为显示Nginx自己的错误页面,并非Tomcat等容器内部所抛出的错误页面。
1、进入配置文件目录
vim /usr/local/nginx/conf/nginx.conf

2、http-->server-->location / 参数下增加以下内容
	proxy_intercept_errors on;

3、http-->server参数下增加或修改以下内容(页面、页面存放路径自行定义)
	error_page 404 /404.html;
	location = /404.html {
		root /usr/local/nginx/error;
	}
	
	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root /usr/local/nginx/error;
	}

4、重新载入配置文件
	/usr/local/nginx/sbin/nginx -s reload

三、配置静态资源访问
1、进入配置文件目录
vim /usr/local/nginx/conf/nginx.conf

2、http-->server参数下增加以下内容
	一、目录匹配方式(表示可访问static/images目录下的文件,例:http://ip:port/images/xx.jpg):
	location ^~ /images/ {
    	root /usr/local/nginx/static;
    }
	二、后缀匹配方式(表示可访问static/others目录下指定后缀的文件,例:http://ip:port/xx.pdf):
	location ~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|pdf)$ {
		root /usr/local/nginx/static/others;
	}
4、重新载入配置文件
	/usr/local/nginx/sbin/nginx -s reload

四、匹配规则介绍
1、语法规则: location [=|||^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示url以某个常规字符串开头,理解为匹配url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
~ 开头表示区分大小写的正则匹配
~
开头表示不区分大小写的正则匹配
!和!*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到

2、匹配顺序:
	首先匹配=,其次匹配^~, 再次是按文件中顺序的正则匹配,最后是交给/通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求
	
3、示例:
	#规则A
	location = / {
	   #访问http://localhost/将匹配规则A
	}
	#规则B
	location = /login {
	   #访问http://localhost/login将匹配规则B
	}
	#规则C
	location ^~ /static/ {
	   #访问http://localhost/static/a.html将匹配规则C
	}
	#规则D
	location ~ \.(gif|jpg|png|js|css)$ {
	   #访问http://localhost/a.gif,http://localhost/b.jpg将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而http://localhost/static/c.png则优先匹配到规则C
	}
	#规则E
	location ~* \.png$ {
	   #访问http://localhost/a.PNG则匹配规则E,而不会匹配规则D,因为规则E不区分大小写
	}
	#规则F
	location / {
	   #访问http://localhost/register将匹配规则F
	}	
	
	五、配置前后端分离URL转发(反向代理)
1、进入配置文件目录
	vim /usr/local/nginx/conf/nginx.conf
	
2、http-->server 参数中增加以下内容
	#设置静态文件目录及默认页面
	location / {
        root /home/static;
        index index.html;
    }
	#设置指定的静态文件都会转发到静态文件目录下
	location ~ .*\.(html|htm|js|css|gif|jpg|jpeg|bmp|png|ico|swf|flv|svg|txt|ttf|woff)$ {
        proxy_set_header Host $host;
    	proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header REMOTE-HOST $remote_addr;
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		root /home/static;
	}
	#设置后端接口服务地址,如有多个Server端,则配置多个location /name/ {}属性
    location ~ .*$ {
        proxy_pass http://172.16.210.229:8080;
    }
	
3、配置完成后重新载入配置文件
	/usr/local/nginx/sbin/nginx -s reload

六、日志配置
1、进入配置文件目录
vim /usr/local/nginx/conf/nginx.conf

2、配置error_log错误日志
	2.1、作用域:
		main、http、server、location
	2.2、可在作用域下添加如下配置(以下为默认配置):
		error_log logs/error.log error;
	2.2、级别:
		debug | info | notice | warn | error | crit | alert | emerg
	
3、配置access_log访问日志
	3.1、作用域:
		http, server, location, if in location, limit_except
	3.2、可在作用域下添加如下配置(以下为默认配置)
		access_log logs/access.log;
		或者:
		access_log logs/access.log main;
	3.3、main为log_format的名称,可自行定义(以下为默认配置)
		log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
						  '$status $body_bytes_sent "$http_referer" '
						  '"$http_user_agent" "$http_x_forwarded_for"';
	3.4、不记录日志:
		access_log off;
		
4、重新载入配置文件
	/usr/local/nginx/sbin/nginx -s reload
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值