1、rewrite
location /keystone {rewrite ^/keystone/(.*)$ /v3/$1 break;proxy_pass http://192.100.3.1:5000;}location /glance {rewrite ^/glance/(.*)$ /v2/$1 break;proxy_pass http://192.100.3.1:9292;}location /nova {rewrite ^/nova/(.*)$ /v2.1/$1 break;proxy_pass http://192.100.3.1:8774;}location /cinder {rewrite ^/cinder/(.*)$ /v3/$1 break;proxy_pass http://192.100.3.1:8776;}location /network {rewrite ^/network/(.*)$ /v2.0/$1 break;proxy_pass http://192.100.3.1:9696;}
2、upstream:负载均衡
#配置负载均衡upstream imageserver{#Round Robin;轮询算法 默认为轮询#least_conn ; 将活动连接最少的请求发送到服务器,再次考虑服务器权重:#ip_hash;server 192.100.3.29:81 weight=1;server 192.100.3.29:82 weight=1;}server{listen 81;server_name 192.100.3.29;root html;access_log logs/81-access.log main;}server{listen 82;server_name localhost;root html;access_log logs/82-access.log main;}server {listen 8080;location ~* \.(jpg|gif|jepg|png){proxy_set_header X-Forwarded-For $remote_addr;proxy_pass http://imageserver;}}
3、stream:四层代理
nginx 从1.9.0版本开始支持四层代理,但做四层代理时 编译需要添加 —with-stream模块./configure --with-stream
官网配置
#和http同级stream {upstream ssh_proxy {server 192.100.5.228:22;}server {listen 2222;proxy_pass ssh_proxy;}server {listen 3333;proxy_pass 192.100.5.222:3306;}}
5、缓存
6、http
7、location
location / {deny 192.168.1.1;allow 192.168.1.0/24;allow 10.1.1.0/16;allow 2001:0db8::/32;deny all;}查看状态location /status {stub_status on;access_log off;allow 192.100.5.115;deny all;}location / {proxy_pass http://192.100.5.222:28080;}location = / {root dist;index index.html index.htm;}location ~* .(html|jpg|png|ico|woff|ttf|css|js)$ {root dist;}
upstream pve.server{ip_hash;server 192.100.3.201:8006 weight=1;server 192.100.3.202:8006 weight=1;server 192.100.3.203:8006 weight=1;}server {listen 1888 ssl;ssl_certificate ssl/server-cert.cer;ssl_certificate_key ssl/server-key.key;server_name localhost;location / {root dist;index index.html index.htm;}location /api3/ {proxy_pass https://pve.server/;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";}location ~ (/api2|/xtermjs|/novnc) {proxy_pass https://pve.server;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";}}
8、fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REQUEST_SCHEME $scheme;fastcgi_param HTTPS $https if_not_empty;fastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;
示例:
#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;#开启main日志格式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;gzip on;gzip_min_length 200;gzip_types text/plain application/xml;#keepalive_timeout 0;keepalive_timeout 65;#配置负载均衡upstream imageserver{server 192.100.3.29:81 weight=1;server 192.100.3.29:82 weight=1;}server{listen 81;server_name 192.100.3.29;root html;access_log logs/81-access.log main;}server{listen 82;server_name localhost;root html;access_log logs/82-access.log main;}#gzip on;server {listen 8080;#root /test/nginx/jpg;location ~* \.(jpg|gif|jepg|png){proxy_set_header X-Forwarded-For $remote_addr;proxy_pass http://imageserver;}location /proxy{alias /test/nginx/jpg;autoindex on;}}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /images {root data; # alias data/imagesautoindex on;}#location /jpg{# root /test/nginx;# autoindex on;#}location = /jpg/2.jpg {root /test/nginx/;autoindex on;}location /proxy{proxy_pass http://127.0.0.1:8080;}#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 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
