NGINX配置

目录

配置

1. 安装最新版nginx

阿里云和腾讯云的apt源都比较老,安装的nginx版本比较低,因此需要升级到新版本的nginx

如果已经安装,先卸载

sudo apt autoremove --pure nginx

替换默认的apt源,安装方法

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:nginx/stable 
sudo apt-get update
sudo apt-get install nginx

查看nginx安装版本

nginx -v
或者
nginx -V

参考: Ubuntu 14.04 安装最新版本Nginx

2. TCP/UDP代理

修改/etc/nginx/nginx.conf内容

修改nginx worker进程最大文件描述符限制,默认为1024

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

# fix default ulimit(1024) issue-too many open files
worker_rlimit_nofile 400000;

增加stream模块,并设备stream模块访问日志

stream {
	log_format proxy '[$time_local] $remote_addr -> $upstream_addr '
		'$protocol status:$status '
		'sent/received:(remote:$bytes_sent/$bytes_received upstream:$upstream_bytes_sent/$upstream_bytes_received) '
		'session_time:$session_time  upstream_connect_time:$upstream_connect_time';

	access_log /var/log/nginx/stream-access.log proxy ;
	open_log_file_cache off;
	
	include /etc/nginx/conf.d/*.stream;
}

添加stream配置项/etc/nginx/conf.d/test.stream

upstream tcp_backend {
	server 1.1.1.1:1001;
	server 2.2.2.2:1002;
}

upstream udp_backend {
	server 1.1.1.1:1003;
	server 2.2.2.2:1004;
}

server {
	listen 1100;
	proxy_timeout  2m;
	proxy_connect_timeout  120s;
	proxy_pass tcp_backend;
}

server {
	listen 2200 udp;
	proxy_timeout  2m;
	proxy_connect_timeout  120s;
	proxy_pass udp_backend;
}