运行一个nginx服务并结合hosts配置,实现80端口代理双域名的功能

305 阅读1分钟

nginx:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
	gzip on;
	#该指令用于开启或关闭gzip模块(on/off)
	gzip_min_length 1k;
	#设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
	gzip_buffers 4 16k;
	#设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
	gzip_http_version 1.1;
	#识别http的协议版本(1.0/1.1)
	gzip_comp_level 2;
	#gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
	gzip_types text/css application/javascript
	#匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
	gzip_vary on;
	#和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

	server {
		listen                80;
		charset              utf-8;

		server_name sso-t.ainnovation.com;
		location ^~ /
			{
				proxy_pass http://192.168.21.231:8888;
				proxy_set_header Host $host;
				proxy_set_header X-Real-IP $remote_addr;
				proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
				proxy_set_header REMOTE-HOST $remote_addr;
				
				add_header X-Cache $upstream_cache_status;
				
				#Set Nginx Cache
				
				
				set $static_fileq2ZIU0k8 0;
				if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
				{
				 set $static_fileq2ZIU0k8 1;
				 expires 12h;
					}
				if ( $static_fileq2ZIU0k8 = 0 )
				{
				add_header Cache-Control no-cache;
				}
			}
	}
	server {
		listen                80;
		charset              utf-8;
		server_name ops-t.ainnovation.com;
		location ^~ /
			{
				proxy_pass http://192.168.21.231:10000;
				proxy_set_header Host $host;
				proxy_set_header X-Real-IP $remote_addr;
				proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
				proxy_set_header REMOTE-HOST $remote_addr;
				
				add_header X-Cache $upstream_cache_status;
				
				#Set Nginx Cache
				
				
				set $static_fileq2ZIU0k8 0;
				if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
				{
				 set $static_fileq2ZIU0k8 1;
				 expires 12h;
					}
				if ( $static_fileq2ZIU0k8 = 0 )
				{
				add_header Cache-Control no-cache;
				}
			}
	}
}

hosts

127.0.0.1       ops-t.ainnovation.com
127.0.0.1       sso-t.ainnovation.com