小李同学 keep digging deeper
Nginx

Nginx基础配置

— 字

先把几个常看的命令放在前面:

# 配置文件位置
ls /usr/local/nginx
ls /usr/local/openresty/nginx/conf

# 看编译了哪些模块
nginx -V

# 看 CPU 核数,用来定 worker_processes
lscpu

嵌套关系很简单:http -> server -> location。

主配置

# nginx 的运行用户,如果写作 nobody nobody代表用户和所属组,比如limingze:limingze
#user  nobody;
# 运行的ork数量,可以设置成cpu的核数,用lscpu命令查看,这里可以改成16
#worker_processes 1;

# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;


#error_log  logs/error.log; #不写默认代表error级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #存放pid的路径

# ulimit -n 查看最多连接数
events {
    worker_connections  1024;
}


http {
    include       mime.types; # vim下gf可以查看所有类型,:bd或者ctrl+o返回
    default_type  application/octet-stream; # 未知类型时,默认的返回类型

    # Enables or disables the use of underscores in client request header fields.
    # When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.
    # underscores_in_headers off;

    server_tokens off; #关闭版本信息输出。curl -I 127.0.0.1,可以看到Server后面跟着版本号


    #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; # main指代log_format,如果打开access_log,上面的log_format也要打开
    # docker 安装后 access.log -> /dev/stdout 日志会重定向,需要在容器外面 docker logs <容器id> 查看


    # See Move default writable paths to a dedicated directory (#119)
    # https://github.com/openresty/docker-openresty/issues/119
    client_body_temp_path /var/run/openresty/nginx-client-body;
    proxy_temp_path       /var/run/openresty/nginx-proxy;
    fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;
    uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;
    scgi_temp_path        /var/run/openresty/nginx-scgi;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    keepalive_requests 100;

    # http Response Headers中,Content-Encoding:gzip
    gzip  on;

    server {
    	# 虚拟主机配置
    }
    include /etc/nginx/conf.d/*.conf;
}

两个容易漏的点:worker_processes 按核数设,worker_connections 乘以 worker 数才是并发上限,还要看 ulimit -n 够不够;server_tokens off 只是不显示版本号,Server 这个头本身还在。

虚拟主机

server {
    	listen 80; # 也可以写作127.0.0.1:80,这时候只有本地才能够访问
    	server_name xiaolee.xyz;

    	charset utf-8; #字符集
    	access_log logs/xiaolee.xyz.log main; #自定义日志路径

    	location / {
    		autoindex on; # 列目录
    		root html; #根路径
    		index index.php index.html index.html; #默认请求文件

    	}

    	error_page 404	/404.html;
    	
    	error_page 500 501 502 503 504 /50x.html;
    	location = /50x.html {
    		root html;
    	}

    }

多域名、多端口、多 IP 就是多写几个 server 块,每个块 listen 和 server_name 不同:

server {
	listen 80;
	server_name xiaolee.xyz;

	root /data/nginx/www;
	index index.php index.html;
}

四层端口转发

与 http {} 平级,插一个 stream {}:

stream {
	include /etc/nginx/tcp.d/*.conf;
}

具体转发写在 tcp.d 里,一个上游一组 server,TCP 和 UDP 各写一个:

upstream clash_7890 {
    hash $remote_addr consistent;
    server 192.168.31.17:7890 weight=5 max_fails=3 fail_timeout=30s;
}

server {
    listen 7890;
    proxy_connect_timeout 1s; # 连接超时时间
    proxy_timeout 3s; # 超时时间
    proxy_pass clash_7890; # 转发目标的IP及端口号
}

server {
    listen 7890 udp;
    proxy_connect_timeout 1s; # 连接超时时间
    proxy_timeout 3s; # 超时时间
    proxy_pass clash_7890; # 转发目标的IP及端口号
}

upstream clash_7891 {
    hash $remote_addr consistent;
    server 192.168.31.17:7891 weight=5 max_fails=3 fail_timeout=30s;
}

server {
    listen 7891;
    proxy_connect_timeout 1s; # 连接超时时间
    proxy_timeout 3s; # 超时时间
    proxy_pass clash_7891; # 转发目标的IP及端口号
}

server {
    listen 7891 udp;
    proxy_connect_timeout 1s; # 连接超时时间
    proxy_timeout 3s; # 超时时间
    proxy_pass clash_7891; # 转发目标的IP及端口号
}

hash $remote_addr consistent 保证同一个客户端 IP 总是落到同一个上游;proxy_timeout 对长连接业务要改大,否则连接会被提前断掉。

防盗链

location ~* \.(jpg|png|gif) {
	valid_referers none blocked www.xiaolee.xyz xiaolee.xyz;
	root html;
	if($invalid_referer){
		#return 403;
		rewrite /(.*) /daotu.jpg break;
	}
}

valid_referers 里列出允许的来源;来源不对时两种处理,直接 return 403 拒绝,或者像上面这样重写成一张占位图。

本文作者 小李同学,转载请注明出处。

评论

还没有人留言。

写一条

提交后会先进站主的待审列表,通过之后才公开。

搜索文章

输入关键词搜索文章。