1、host配置
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
127.0.0.1 test.com
127.0.0.1 www.baidu.com
127.0.0.1 conf
127.0.0.1 re
127.0.0.1 pro
127.0.0.1 rs
127.0.0.1 r1
127.0.0.1 r2
2、nginx.conf配置
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name test.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.baidu.com;
location /{
index index.html index.htm;
root html;
}
location /ori{#重定向到内部
rewrite /ori http://localhost;
}
location /pic{#静态
root /usr;
}
location /download{#下载
add_header Content_Disposition "attachment;";
alias /usr/download/;
}
}
include /usr/nginx/conf/vhost/*.conf;#配置virtual host
upstream real_server{#集群负载均衡策略:轮询 优点:服务挂掉能自动剔除 同一台机器上端口必须不同,不管是用域名还是ip,不同机器上可以相同
server r1:8080;#127.0.0.1:8080;
server r2:8090;#127.0.0.1:8090;
}
#upstream real_server{#集群负载均衡策略:权重 优点:服务器性能不均
#server r1:8080 weight=60;
#server r2:8090 weight=40;
#}
#upstream real_server{#集群负载均衡策略:ip hash 优点:可以解决session问题。原因:同一用户始终访问同一点
#ip_hash;
#server r1:8080;
#server r2:8090;
#}
#upstream real_server{#需第三方模块支持 优点:根据响应时间计算访问点
#server r1:8080;
#server r2:8090;
#fair;
#}
#upstream real_server{#需第三方模块支持 优点:后端服务为缓存时比较有优势
#server r1:8080;
#server r2:8090;
#hash $request_uri;
#hash_method crc32;
#}
server {
listen 8080;
server_name r1;
location /{
index index.html index.htm;
root html;
}
}
server {
listen 8090;
server_name r2;
location /{
index test.html;
root html;
}
}
server {
listen 80;
server_name re;
location =/{
index index.html index.html;
root html;
}
location /re0 {#重写#引起url改变,如不想改变,可以采用反向代理
rewrite /re0 / break;#终止下面的匹配
}
location ~ ^/re2{#重写之后地址改变,返回url改变,然后匹配到re3,使用curl -L http://re/re2测试,重定向到re3.并且请求变为/html/re3
rewrite /re2 /re3 break;
}
location =/re3{
root html;
index index.html index.htm;
}
location /logl{
index index.html index.htm;
root html;
}
location =/re4/{#反向代理,执行同一server的其他地址 proxy_pass的url后面如果加上/不会把匹配的路径带走,否则会带上 proxy_pass也可指定IP
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_pass http://re/re3/;
}
location /re6{#重写到反向代理的匹配
rewrite /re6 /re4;
}
location /re8/{
root html;
index index.html index.htm;
}
location =/re7 {#重写地址为re8,并做反向代理,url地址不发生化.如去掉rewrite,url地址改变
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /re7 /re8/;
proxy_pass http://re;
}
location /re9{#反向代理到集群
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_pass http://real_server/;
}
}
server {
listen 80;
server_name rs;
location /{
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_pass http://real_server;
}
}
server {
listen 80;
server_name pro;
location /{
index test.html;
root html;
}
location /a/{
index test.html
root html;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}