Nginx基本配置和作用


nginx可以重新加载文件的。我们直接运行:nginx -s reload 

配置文件有没有问题,可以直接输入:nginx -t

nginx -s stop就可以关闭

但有时我们就不想它挂的时候访问另外一个,而只是希望一个服务器访问的机会比另外一个大,使用weight

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #gzip  on;       #一个服务器挂了,多配置一个jetty,weight=数字来指定,数字越大,表明请求到的机会越大       upstream local_tomcat {              server localhost:8080 weight=1;             server localhost:9999 weight=5;     server{         listen       8030;         server_name  localhost:8080;         #charset koi8-r;        #access_log  logs/host.access.log  main;         location / {proxy_pass http://local_tomcat;}          ##......其他省略         #过滤不同的jsp和静态html页面         #location / {  proxy_pass http://localhost:8080;}         #location ~ .jsp$ {    proxy_pass http://localhost:8080;}          #location ~ .(html|js|css|png|gif)$ {  root D:/apache-tomcat-7.0.77/webapps/ROOT;}         error_page   500 502 503 504  /50x.html;           location = /50x.html {               root   html;           }       }
 

1、nginx能做反向代理,那么什么是反向代理呢,举个栗子,我想在本地使用 www.mickey.com 的域名去访问 www.taobao.com。那么这个时候我们就可以通过nginx去实现。

2、nginx能实现负载均衡,什么是负载均衡呢?就是我的项目部署在不同的服务器上,但是通过统一的域名进入,nginx则对请求进行分发,减轻了服务器的压力。

3、作为安全隔离的作用;

4、解决跨域问题。

5、缓存静态文件,加快访问速度。

修改nginx.conf 配置文件,启用 upstream 负载均衡 tomcat Cluster,默认使用轮询方式。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 upstream site {      server localhost:8080;     server localhost:9090;   server {     listen       80;     server_name  localhost;       #charset koi8-r;     #access_log  /var/log/nginx/log/host.access.log  main;       location / {         #root   /usr/share/nginx/html;         #index  index.html index.htm;         index  index_tel.jsp index.jsp index.html index.htm ;          proxy_redirect          off;            proxy_set_header        Host            $host;            proxy_set_header        X-Real-IP       $remote_addr;            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;            client_max_body_size    10m;            client_body_buffer_size 128k;            proxy_buffers           32 4k;          proxy_connect_timeout   3;            proxy_send_timeout      30;            proxy_read_timeout      30;               proxy_pass http://site;       }
 

测试:

1、访问 http://10.129.221.70:8080 直接请求到tomcat_1服务器,显示 “ response from tomcat_1 ”, session 值为 ‘56E2FAE376A47F1C0961D722326B8423’;

2、访问 http://10.129.221.70:9090 直接请求到tomcat_2服务器,显示 “ response from tomcat_2 ”, session 值为 ‘56E2FAE376A47F1C0961D722326B8423’;

3、访问 http://10.129.221.70 (默认80端口)请求到 nginx 反向代理到指定Web服务器,由于默认使用轮询负载方式,反复刷新页面显示的内容在“ response from tomcat_1 ” 和 “ response from tomcat_2 ”之间切换,但 session 值保持为 ‘56E2FAE376A47F1C0961D722326B8423’;

4、使用 redis-cli 连接 redis 服务器,查看会显示有 “56E2FAE376A47F1C0961D722326B8423” key的 session 数据,value为序列化数据。

相关