初识Nginx

前言

【PS:原文手打,转载说明出处,博客园

未完待续)在互联网行业,为了高并发,高可用,服务器就必须做负载,可以是硬件负载,也可以是软件负载,各种方案很多,最直接的就是购买阿里云的SLB对应在购买ECS就可以了。

本文介绍的是自己从无到有搭建Nginx,因为是NG新手,所以有错误的地方多担待。

作案工具

  • center os 6.5
  • xshell
  • nginx version: nginx/1.14.0

网络拓扑图(套路)

 

 

 

Center OS 6.5安装 Nginx

1:创建nginx.repo文件

cd /etc/yum.repos.d
vim nginx.repo
#写入下面内容至文件
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

2:安装nginx

yum install nginx -y
service nginx start
#浏览器输入linux服务器IP,如果正常显示ng页面则说明正常,如果不显示说明80端口没开通,开通代码80端口如下
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
service iptables save
service iptables restart

 

3:查看执行进程(PS:常规几个线程的cpu默认为几个工作进程加上一个master的守护进程)

4:停用启用ng服务

service nginx start #启用
service nginx stop #停用
service nginx restart #重启

 

配置服务器

cd /etc/nginx
vi nginx.conf

 1:单域名配置

user nginx;
worker_processes 2;#工作进程数,默认为CPU线程数
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;#工作最大连接数
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
#关键在这 这个是路由跳转地址,也就是流量进来后,需要跳转到哪一台服务器,服务器有好坏之分,weight值越大,做的事情越多 还有最大的失败数以及失败超时时间设置,当比如111服务器停止了,ng会自动停掉所有流量打向144服务器
upstream iis_pool
{
server 10.2.1.111:8011 weight=4 max_fails=2 fail_timeout=10s;#当连续两次访问不到服务器时,则不在访问
server 10.2.1.144:8011 weight=4 max_fails=2 fail_timeout=10s;
}
server
{
#监听端口
listen 80;
#监听的地址
server_name ng.zhoudemo.com;
#所有域名请求地址使用代理规则
location / {
proxy_pass http://iis_pool;
}
}
}
2:多域名配置
user  nginx;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

    upstream iis_pool
    {
       server 10.2.1.111:8011 weight=4 max_fails=2 fail_timeout=10s;
        server 10.2.1.144:8011 weight=4 max_fails=2 fail_timeout=10s;
    }
    upstream iis2_pool
    {
       server 10.2.1.111:8022 weight=4 max_fails=2 fail_timeout=10s;
        server 10.2.1.144:8022 weight=4 max_fails=2 fail_timeout=10s;
    }

    server
    {
      listen 80;
      server_name ng.zhoudemo.com;
   
        location / {
                proxy_pass http://iis_pool;
        }
    }

    server
    {
      listen 80;
      server_name ng2.zhoudemo.com;

        location / {
                proxy_pass http://iis2_pool;
        }
    }
}

测试结果

 

 

 

 

posted @ 2018-05-09 22:30  随风ˇ止步  阅读(356)  评论(0编辑  收藏  举报