Nignx配置

Nginx配置之nginx.conf文件解析及配置

1、nginx.conf文件解析

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##
	#开启文件的高效传输模式
	sendfile on;
	#防止网络阻塞
	tcp_nopush on;
	#防止延迟
	tcp_nodelay on;
	#超时时长 单位秒
	keepalive_timeout 65;
	#每次连接最大字节数
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;
	#包含文件传输类型
	include /etc/nginx/mime.types;
	#默认传输类型
	default_type application/octet-stream;

	##
	# SSL Settings
	##
	#支持https
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##
	#日志位置
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##
	#保持压缩输出
	gzip on;
	#压缩禁止类型
	gzip_disable "msie6";

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##
	#包含配置
	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

全局配置:
user:工作用户
Worker_process:工作进程数,理论上越大越好。auto与cpu进程数一致
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

2、新建conf配置

1、在/etc/nginx/conf.d新建配置文件 sudo vi 01_listen.conf

在这里插入图片描述
2、编写server配置和index可自定义
在这里插入图片描述Root
4、保存后检查配置文件是否有误 nginx -t
5、重启nginx服务 systemctl restart nginx
6、查看监听状态 netstat -tunlp |grep nginx
在这里插入图片描述在这里插入图片描述

3、root和alias区别在这里插入图片描述在这里插入图片描述

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在location /后加后缀,使用root时,是拼接到root配置路径中;使用alias,不拼接,可以访问ip地址加端口/后缀

##4、 Server_name配置
当有多个server监听同一端口,通过server_name区分

5、location匹配

1、匹配格式

类型含义匹配方式优先级样式
=/路径精确匹配前缀1location=/image{}
^~优先匹配前缀2location^~/page{}
~普通正则-大小写敏感正则符号3location~(.jpe?g)${}
~*普通正则-大小写不敏感正则符号3location ~*.(jpe?g)${}
空/通用匹配前缀4location /{}
空<路径>通用匹配前缀*location /index{}

2、匹配优先级
精确匹配>location 完整路径>优先匹配>正则匹配>location 部分路径>通用匹配
3、匹配示例
#精确匹配
Location =/{
}
location =/login {
}
#优先匹配
Location ^~ /static/ {
}

#正则匹配
location ~ .(gif|jpg|png|js|css) ${
}

Location ~* .png ${
}

#通用规则
location /{
}

案例:

location /{
 # return 302 http://192.168.5.99;
#         allow 192.168.31.50/24;
#         deny all;
#         return 302 /login;
        root /etc/nginx;
    autoindex on;
}
location @aaaa{
return 404;
}
location /login{
 return 302 http://192.168.5.99;
}
location = /login_10{
 return 302 http://192.168.5.195:8092/sysware/;
}
location ^~ /static/ {
return 403;
}
location ~ \.(gif|jpg|png)$ {
return 404;
}
location ~* \.png$ {
return 500;
}

6、Try_files

作用:可以使用try_files进行异常处理临时跳转
##找不到静态资源则返回404
Try_files $uri $uri/=404;

location =/ {
Try_files $uri @aaaa
}

Location @aaaa{
return 404;
}

7、Location 临时跳转

作用:可以使用302代号,进行内部、外部重定向跳转

内部重定向:
Location =/{
Return 302 /login;
}
Location /login {
Return 302 http://www.baidu.com
}

外部重定向:
Location =/{
return 302 http://www.baidu.com
}

Location 临时跳转
作用:可以使用302代号,进行内部、外部重定向跳转

内部重定向:
Location =/{
Return 302 /login;
}
Location /login {
Return 302 http://www.baidu.com
}

外部重定向:
Location =/{
return 302 http://www.baidu.com
}

8、Location 访问控制

作用:可以配置deny属性,允许或者阻止ip地址的访问
location / {
: deny 192.168.1.1;
: allow 192.168.1.0/24;
: allow 10.1.1.0/16;
: deny all;
}

9、Location 目录列表 autoindex

作用:可以通过sutoindex配置,进行文件访问和下载
在这里插入图片描述
location /{
root /etc/nginx;
autoindex on;
autoindex_localtime on;
}
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值