1、rewrite

链接

  1. location /keystone {
  2. rewrite ^/keystone/(.*)$ /v3/$1 break;
  3. proxy_pass http://192.100.3.1:5000;
  4. }
  5. location /glance {
  6. rewrite ^/glance/(.*)$ /v2/$1 break;
  7. proxy_pass http://192.100.3.1:9292;
  8. }
  9. location /nova {
  10. rewrite ^/nova/(.*)$ /v2.1/$1 break;
  11. proxy_pass http://192.100.3.1:8774;
  12. }
  13. location /cinder {
  14. rewrite ^/cinder/(.*)$ /v3/$1 break;
  15. proxy_pass http://192.100.3.1:8776;
  16. }
  17. location /network {
  18. rewrite ^/network/(.*)$ /v2.0/$1 break;
  19. proxy_pass http://192.100.3.1:9696;
  20. }

2、upstream:负载均衡

upstream

  1. #配置负载均衡
  2. upstream imageserver{
  3. #Round Robin;轮询算法 默认为轮询
  4. #least_conn ; 将活动连接最少的请求发送到服务器,再次考虑服务器权重:
  5. #ip_hash;
  6. server 192.100.3.29:81 weight=1;
  7. server 192.100.3.29:82 weight=1;
  8. }
  9. server{
  10. listen 81;
  11. server_name 192.100.3.29;
  12. root html;
  13. access_log logs/81-access.log main;
  14. }
  15. server{
  16. listen 82;
  17. server_name localhost;
  18. root html;
  19. access_log logs/82-access.log main;
  20. }
  21. server {
  22. listen 8080;
  23. location ~* \.(jpg|gif|jepg|png){
  24. proxy_set_header X-Forwarded-For $remote_addr;
  25. proxy_pass http://imageserver;
  26. }
  27. }

3、stream:四层代理

nginx 从1.9.0版本开始支持四层代理,但做四层代理时 编译需要添加 —with-stream模块
./configure --with-stream
官网配置

  1. #和http同级
  2. stream {
  3. upstream ssh_proxy {
  4. server 192.100.5.228:22;
  5. }
  6. server {
  7. listen 2222;
  8. proxy_pass ssh_proxy;
  9. }
  10. server {
  11. listen 3333;
  12. proxy_pass 192.100.5.222:3306;
  13. }
  14. }

5、缓存

image.png

6、http

链接

7、location

链接

  1. location / {
  2. deny 192.168.1.1;
  3. allow 192.168.1.0/24;
  4. allow 10.1.1.0/16;
  5. allow 2001:0db8::/32;
  6. deny all;
  7. }
  8. 查看状态
  9. location /status {
  10. stub_status on;
  11. access_log off;
  12. allow 192.100.5.115;
  13. deny all;
  14. }
  15. location / {
  16. proxy_pass http://192.100.5.222:28080;
  17. }
  18. location = / {
  19. root dist;
  20. index index.html index.htm;
  21. }
  22. location ~* .(html|jpg|png|ico|woff|ttf|css|js)$ {
  23. root dist;
  24. }
  1. upstream pve.server{
  2. ip_hash;
  3. server 192.100.3.201:8006 weight=1;
  4. server 192.100.3.202:8006 weight=1;
  5. server 192.100.3.203:8006 weight=1;
  6. }
  7. server {
  8. listen 1888 ssl;
  9. ssl_certificate ssl/server-cert.cer;
  10. ssl_certificate_key ssl/server-key.key;
  11. server_name localhost;
  12. location / {
  13. root dist;
  14. index index.html index.htm;
  15. }
  16. location /api3/ {
  17. proxy_pass https://pve.server/;
  18. proxy_http_version 1.1;
  19. proxy_set_header Upgrade $http_upgrade;
  20. proxy_set_header Connection "Upgrade";
  21. }
  22. location ~ (/api2|/xtermjs|/novnc) {
  23. proxy_pass https://pve.server;
  24. proxy_http_version 1.1;
  25. proxy_set_header Upgrade $http_upgrade;
  26. proxy_set_header Connection "Upgrade";
  27. }
  28. }

8、fastcgi_params

  1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  2. fastcgi_param QUERY_STRING $query_string;
  3. fastcgi_param REQUEST_METHOD $request_method;
  4. fastcgi_param CONTENT_TYPE $content_type;
  5. fastcgi_param CONTENT_LENGTH $content_length;
  6. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  7. fastcgi_param REQUEST_URI $request_uri;
  8. fastcgi_param DOCUMENT_URI $document_uri;
  9. fastcgi_param DOCUMENT_ROOT $document_root;
  10. fastcgi_param SERVER_PROTOCOL $server_protocol;
  11. fastcgi_param REQUEST_SCHEME $scheme;
  12. fastcgi_param HTTPS $https if_not_empty;
  13. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  14. fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
  15. fastcgi_param REMOTE_ADDR $remote_addr;
  16. fastcgi_param REMOTE_PORT $remote_port;
  17. fastcgi_param SERVER_ADDR $server_addr;
  18. fastcgi_param SERVER_PORT $server_port;
  19. fastcgi_param SERVER_NAME $server_name;

示例:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #开启main日志格式
  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. gzip on;
  21. gzip_min_length 200;
  22. gzip_types text/plain application/xml;
  23. #keepalive_timeout 0;
  24. keepalive_timeout 65;
  25. #配置负载均衡
  26. upstream imageserver{
  27. server 192.100.3.29:81 weight=1;
  28. server 192.100.3.29:82 weight=1;
  29. }
  30. server{
  31. listen 81;
  32. server_name 192.100.3.29;
  33. root html;
  34. access_log logs/81-access.log main;
  35. }
  36. server{
  37. listen 82;
  38. server_name localhost;
  39. root html;
  40. access_log logs/82-access.log main;
  41. }
  42. #gzip on;
  43. server {
  44. listen 8080;
  45. #root /test/nginx/jpg;
  46. location ~* \.(jpg|gif|jepg|png){
  47. proxy_set_header X-Forwarded-For $remote_addr;
  48. proxy_pass http://imageserver;
  49. }
  50. location /proxy{
  51. alias /test/nginx/jpg;
  52. autoindex on;
  53. }
  54. }
  55. server {
  56. listen 80;
  57. server_name localhost;
  58. #charset koi8-r;
  59. #access_log logs/host.access.log main;
  60. location / {
  61. root html;
  62. index index.html index.htm;
  63. }
  64. location /images {
  65. root data; # alias data/images
  66. autoindex on;
  67. }
  68. #location /jpg{
  69. # root /test/nginx;
  70. # autoindex on;
  71. #}
  72. location = /jpg/2.jpg {
  73. root /test/nginx/;
  74. autoindex on;
  75. }
  76. location /proxy{
  77. proxy_pass http://127.0.0.1:8080;
  78. }
  79. #error_page 404 /404.html;
  80. # redirect server error pages to the static page /50x.html
  81. #
  82. error_page 500 502 503 504 /50x.html;
  83. location = /50x.html {
  84. root html;
  85. }
  86. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  87. #
  88. #location ~ \.php$ {
  89. # proxy_pass http://127.0.0.1;
  90. #}
  91. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  92. #
  93. #location ~ \.php$ {
  94. # root html;
  95. # fastcgi_pass 127.0.0.1:9000;
  96. # fastcgi_index index.php;
  97. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  98. # include fastcgi_params;
  99. #}
  100. # deny access to .htaccess files, if Apache's document root
  101. # concurs with nginx's one
  102. #
  103. #location ~ /\.ht {
  104. # deny all;
  105. #}
  106. }
  107. # another virtual host using mix of IP-, name-, and port-based configuration
  108. #
  109. #server {
  110. # listen 8000;
  111. # listen somename:8080;
  112. # server_name somename alias another.alias;
  113. # location / {
  114. # root html;
  115. # index index.html index.htm;
  116. # }
  117. #}
  118. # HTTPS server
  119. #
  120. #server {
  121. # listen 443 ssl;
  122. # server_name localhost;
  123. # ssl_certificate cert.pem;
  124. # ssl_certificate_key cert.key;
  125. # ssl_session_cache shared:SSL:1m;
  126. # ssl_session_timeout 5m;
  127. # ssl_ciphers HIGH:!aNULL:!MD5;
  128. # ssl_prefer_server_ciphers on;
  129. # location / {
  130. # root html;
  131. # index index.html index.htm;
  132. # }
  133. #}
  134. }