1.下载nginx的压缩包,可以去官网下载

2.解压缩,可以看到其中有个conf的文件夹,在该目录中,nginx.conf配置文件就是核心配置文件

3.默认配置

  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;
  9.  
  10. events {
  11. worker_connections 1024;
  12. }
  13.  
  14. http {
  15. include mime.types;
  16. default_type application/octet-stream;
  17.  
  18. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  19. # '$status $body_bytes_sent "$http_referer" '
  20. # '"$http_user_agent" "$http_x_forwarded_for"';
  21.  
  22. #access_log logs/access.log main;
  23.  
  24. sendfile on;
  25. #tcp_nopush on;
  26.  
  27. #keepalive_timeout 0;
  28. keepalive_timeout 65;
  29.  
  30. #gzip on;
  31.  
  32. server {
  33. listen 80;
  34. server_name localhost;
  35.  
  36. #charset koi8-r;
  37.  
  38. #access_log logs/host.access.log main;
  39.  
  40. location / {
  41. root html;
  42. index index.html index.htm;
  43. }
  44.  
  45. #error_page 404 /404.html;
  46.  
  47. # redirect server error pages to the static page /50x.html
  48. #
  49. error_page 500 502 503 504 /50x.html;
  50. location = /50x.html {
  51. root html;
  52. }
  53.  
  54. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  55. #
  56. #location ~ \.php$ {
  57. # proxy_pass http://127.0.0.1;
  58. #}
  59.  
  60. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  61. #
  62. #location ~ \.php$ {
  63. # root html;
  64. # fastcgi_pass 127.0.0.1:9000;
  65. # fastcgi_index index.php;
  66. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  67. # include fastcgi_params;
  68. #}
  69.  
  70. # deny access to .htaccess files, if Apache's document root
  71. # concurs with nginx's one
  72. #
  73. #location ~ /\.ht {
  74. # deny all;
  75. #}
  76. }
  77.  
  78. # another virtual host using mix of IP-, name-, and port-based configuration
  79. #
  80. #server {
  81. # listen 8000;
  82. # listen somename:8080;
  83. # server_name somename alias another.alias;
  84.  
  85. # location / {
  86. # root html;
  87. # index index.html index.htm;
  88. # }
  89. #}
  90.  
  91. # HTTPS server
  92. #
  93. #server {
  94. # listen 443 ssl;
  95. # server_name localhost;
  96.  
  97. # ssl_certificate cert.pem;
  98. # ssl_certificate_key cert.key;
  99.  
  100. # ssl_session_cache shared:SSL:1m;
  101. # ssl_session_timeout 5m;
  102.  
  103. # ssl_ciphers HIGH:!aNULL:!MD5;
  104. # ssl_prefer_server_ciphers on;
  105.  
  106. # location / {
  107. # root html;
  108. # index index.html index.htm;
  109. # }
  110. #}
  111.  
  112. }

4.配置负载后的配置文件

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

5.配置解释

1)upstream 用来配置上游负载均衡的真实服务器

2)location:

proxy_pass:用来配置代理转发的请求目标地址

以下部分的配置是防止在nginx的代理转发之后,web界面无法加载js和css

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

root 、alias指令区别

  1. location /img/ {
  2. alias /var/www/image/;
  3. }
  1. #若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
  1. location /img/ {
  2. root /var/www/image;
  3. }
  1. #若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。]

alias是一个目录别名的定义,root则是最上层目录的定义。

还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~

上述路径针对的是nginx的目录,而不是负载后的服务器中的目录

nginx的负载均衡配置的更多相关文章

  1. nginx四层负载均衡配置

    nginx四层负载均衡配置代理Mysql集群 环境如下: ip 192.168.6.203 Nginx ip 192.168.6.*(多台) Mysql 步骤一 查看Nginx是否安装stream模块 ...

  2. Nginx安装负载均衡配置 fair check扩展

    前言 本文主要是针对Nginx安装.负载均衡配置,以及fair智能选举.check后端节点检查扩展功能如何扩展,进行讲解说明. fair模块: upstream-fair,“公平的”Nginx 负载均 ...

  3. Nginx + Tomcat 负载均衡配置详解

    Nginx常用操作指南一.Nginx 与 Tomcat 安装.配置及优化1. 检查和安装依赖项 yum -y install gcc pcre pcre-devel zlib zlib-devel o ...

  4. Nginx+tomcat负载均衡配置

    Nginx+tomcat是目前主流的java web架构,如何让nginx+tomcat同时工作呢,也可以说如何使用nginx来反向代理tomcat后端均衡呢?直接安装配置如下: 1.JAVA JDK ...

  5. CentOS6.5安装nginx及负载均衡配置

    所有的安装包可以去以下地址下载,或者自行去官网下载,下面都有介绍. 所有安装包地址:http://download.csdn.net/detail/carboncomputer/9238037 原文地 ...

  6. nginx的负载均衡配置,常用策略

    场景:nginx是一款非常优秀的负载均衡服务器,小巧而且性能强悍,中小型企业的首选. 下面介绍nginx的负载均衡的几种常见的配置以及优缺点 第一种:轮询(默认) 优点:实现简单 缺点:不考虑每台服务 ...

  7. centos6 Nginx+Tomcat负载均衡配置

    一.Nginx简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤 二.Nginx安装 1.下载N ...

  8. Nginx的负载均衡配置(七)

    原文链接:https://www.cnblogs.com/knowledgesea/p/5199046.html 首先给大家说下upstream这个配置的,这个配置是写一组被代理的服务器地址,然后配置 ...

  9. Linux记录-Nginx+Tomcat负载均衡配置

    Nginx负载均衡配置及策略: 轮询(默认) 优点:实现简单缺点:不考虑每台服务器的处理能力配置示例如下:upstream www.xxx.com {# 需要负载的server列表server www ...

  10. Nginx之负载均衡配置(一)

    前文我们聊了下nginx作为反向代理服务器,代理后端动态应用服务器的配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12430543.html:今天我们来聊 ...

随机推荐

  1. Real-time Compressive Tracking

    这是RTC算法的文献blog Real-time Compressive Tracking Kaihua Zhang1, Lei Zhang1, Ming-Hsuan Yang2 1Dept. of ...

  2. Struts2学习-jsp中超链接传参问题

    今天在学习过程中对struts2中超链接的传参问题产生了一些疑惑,不明白jsp中的超链接如何将参数传到Action方法中去的. <s:iterator value="categorys ...

  3. HDU1312 Red and Black(dfs+连通性问题)

    这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...

  4. mysql对表中数据根据某一字段去重

    要删除重复的记录,就要先查出重复的记录,这个很容易做到 注意:这是查出所有重复记录的第一条记录,需要保留,因此需要添加查询条件,查出所有的重复记录 ) ) 然后 delete from cqssc w ...

  5. linux添加新的环境变量

    Linux下设置PYTHONPATH环境变量有三种方法:一种作用于当前终端,一种作用于当前用户,一种作用于所有用户. 1.作用于当前终端,直接当前终端输入命令 $ export PYTHONPATH= ...

  6. 虫师自动化测试robot Framework 框架的学习

    1.python关键字的定义 #coding=utf-8 def add(a,b): return a+b if __name__ == "__main__": c = add(4 ...

  7. C语言-字符串典型问题分析

    1.典型问题一 下面的程序输出什么为什么? #include <stdio.h>      int main()   {       ] = {};       char src[] =  ...

  8. css+div上下左右自适应居中

    主要记录自己日常积累的布局相关的东西,持续更新中. 1.登录框上下左右自适应居中 以前想要把登录表单始终放置在页面的中间,花了不少心思,一直以来用的解决方法都是用js,感觉有点麻烦不是很好,于是在网上 ...

  9. 利用python装饰器为字符串添加,HTML标签

    # 为字符串添加HTML标签 import time def zhuang(fun): def zhaung_1(*args, **kargs): # time.sleep(1) html_str = ...

  10. 在多租户(容器)数据库中如何创建PDB:方法6 DBCA本地克隆PDB

    基于版本:19c (12.2.0.3) AskScuti 创建方法:DBCA静默本地克隆PDB.根据 CDB1 中的 PDB1 克隆出 CDB1 中的 PDB_CLONE 对应路径:Creating ...