Nginx配置(九)

Nginx 安装

Nginx 安装前需要先安装 pcre、openssl、zlib 等模块,手动安装比较繁琐,所以推荐使用yum 进行安装
1、 默认情况下 CentOS7 中没有 nginx 的下载源,先手动添加官方源

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2、 安装 Nginx

yum install -y nginx

3、Nginx目录结构

[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

Nginx 配置(主配置文件,不建议在此配置项目信息)

  • 主配置文件 /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;      // 当前系统的 CPU 核,通过 lscpu 查看cpu信息

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    // 每个工人最大连接数,一个Nginx的最大连接数= worker_connections*worker_processes
}


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;
    // 引入 /etc/nginx/conf.d/ 下所有后缀为 .conf 的项目配置文件
}

Nginx 配置项目信息

在 nginx 中 conf.d 目录下有一个 default.conf 文件,每个项目都可以复制得到一个独立的配置文件来配置了项目的访问方式

default.conf

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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   /usr/share/nginx/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;
    #}
}

我们使用命令 cp default.conf oa.conf 得到本项目的配置文件oa.conf

  • 将 server_name 修改为 www.testoa.com(根据实际情况修改),access.log 的注释去掉,后面文件名称改为项目名称
  • 将 location 中的 root、index 注释掉,新增 proxy_pass http://www.testoa.com;
upstream www.testoa.com{
    server 192.168.1.28:8080 max_fails=2 fail_timeout=30s;
    server 192.168.1.31:8080 max_fails=2 fail_timeout=30s;
    ip_hash;
}
server {
    listen       80;
    server_name  www.testoa.com;

    access_log  /var/log/nginx/testoa.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        proxy_pass  http://www.testoa.com;
    }

    #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   /usr/share/nginx/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;
    #}
}

注意:

  1. 紫色字体根据实际情况修改,两个 IP 为本项目部署在哪些服务器上
  2. proxy_pass 后的域名名称必须和 upstream 名称保持一致

  • conf.d 目录下可以放多个项目,配置方法同上

  • 启动 nginx

# 在任意目录下执行命令,如果没有任何报错,就代表启动成功了

nginx  或 start nginx

nginx -s reload     // 重启 nginx
nginx -s stop       //停止 nginx
  • 在本机(Windows/Mac),修改 host 文件,配置两个项目的 ip 和域名映射关系
Windows 系统 host 文件在: C:\Windows\System32\drivers\etc\hosts
Mac 系统在:/etc/hosts

hosts文件

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host


# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

192.168.1.33     www.testoa.com          # 新增的域名与Nginx所在机器的IP映射

  • 在浏览器中,通过域名的方式,分别访问两个项目,搞定

Nginx 访问日志配置

用户每次访问 nginx,都会在项目的 access.log 里记录一行日志。可以在访问日志里记录请求耗时,这样方便排查问题
配置方法
在 Nginx 的主配置文件中


#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;   // 每个worker的最大连接数,Nginx的最大连接数为	
	worker_processes*worker_connections
}


http {
    include       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" "$request_time"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

重启 nginx,在项目 access.log 里,可以看到每个请求的耗时了

Nginx 负载均衡策略配置

在 upstream 模块中,添加负载均衡策略

四种策略:

1、轮询:默认策略
2、ip_hash:根据 ip 进行 hash 算法,固定的 ip 分配到固定的后端 server
3、fair:根据后端 server 的响应时间来分配请求,响应时间短的优先分配
4、url_hash:根据 url 进行 hash 算法,固定的 url 分配到固定的后端 server
posted @ 2021-07-25 23:44  DeyouKong  阅读(66)  评论(0编辑  收藏  举报