nginx虚拟主机配置和反向代理
虚拟主机有三种配置方式(常用于本机测试使用,将一个ip和端口的请求根据域名不同分配到不同的应用服务器)
基于ip 不常用
基于端口 可选
基于域名 可选
1 #user nobody; 2 worker_processes 1; #进程数目,和处理器数目一样最好 3 4 #error_log logs/error.log; #错误日志打印目录 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; #进程ip 9 10 11 events { 12 worker_connections 1024; #每个进程最大允许连接数 13 } 14 15 16 http { 17 include mime.types; #允许httpmime类型 18 default_type application/octet-stream; #默认类型,二进制流 19 20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 # '$status $body_bytes_sent "$http_referer" ' 22 # '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 #access_log logs/access.log main; 25 26 sendfile on; #允许发送文件 27 #tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; 31 32 #gzip on; #允许gzip压缩 33 34 #默认服务器地址 35 server { 36 listen 80; 37 server_name localhost; 38 #charset utf-8; #编码 39 40 #access_log logs/host.access.log main; 41 42 # 资源地址 43 location / { 44 root /home/ftp/images; 45 index index.html index.htm; 46 } 47 48 #error_page 404 /404.html; 49 50 # redirect server error pages to the static page /50x.html 51 # 52 error_page 500 502 503 504 /50x.html; 53 location = /50x.html { 54 root html; 55 } 56 57 } 58 59 #根据端口虚拟 60 server { 61 listen 8080; 62 server_name localhost; 63 64 location / { 65 root html; 66 index index.html index.htm; 67 } 68 } 69 70 #根据域名虚拟 71 server { 72 listen 80; 73 server_name xxx.taotao.com; 74 75 location / { 76 root html-xxx; 77 index index.html index.htm; 78 } 79 } 80 81 server { 82 listen 80; 83 server_name yyy.taotao.com; 84 85 location / { 86 root html-yyy; 87 index index.html index.htm; 88 } 89 } 90 91 }