开发运维日记 (一) Nginx部署和内外网配置

本次案例很特别,来到新公司之后接手了一个项目。作为开发99%情况下都不需要过多考虑基础设施的安装配置,比如Nginx、keepalived、mysql等基础设施安装配置,故而通过以下几篇日记来记录项目上线所面临的挑战:

  (一) Nginx部署 和 内外网络配置

  (二) keepalived实现VIP和nginx可用性检查

  (三) Mysql高可用主从配置

1、安装环境:centos7 Nginx 1.16.1
2、主机: 192.168.30.7 从机:192.168.30.8

 

(一) Nginx部署 和 内外网络配置

1、安装Nginx 具体安装shell脚本 nginx_install.sh 


 

#!/bin/bash

echo 'begin install nginx'

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

tar -zxvf nginx-1.16.1.tar.gz

cd nginx-1.16.1

mkdir -p /var/application/nginx

./configure --prefix=/usr/local/nginx

make && make install

echo 'end install nginx'

 注意点:

(1)需要将nginx-1.16.1.tar.gz 和 nginx_install.sh放在一个目录

(2)prefix=/usr/local/nginx 为自定义nginx的安装目录,可以自由指定

(3)/var/application/nginx存放nginx日志目录,可以自由指定

 

2、网络拓扑 和 nginx.conf 配置 


 网络拓扑图如下:

 nginx.conf 配置

#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;
}


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"'
                      'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # nginx ip: 192.168.30.7/8
    # vip: 192.168.30.77

    upstream myservice {
        server 192.168.30.7:8080;
        server 192.168.30.8:8080;
    }

    # 对外
    server {
            listen 80;
            listen [::]:80 default_server;

            #root /var/www/html;

            # Add index.php to the list if you are using PHP
            # index index.html index.htm index.nginx-debian.html;

			location / {
				root   html;
				index  index.html index.htm;
			}

            location /path/api/ {
                proxy_pass http://myservice;
                proxy_read_timeout 300;
                proxy_connect_timeout 300;

                proxy_set_header   X-Forwarded-Proto $scheme;
                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;
            }

            location /path/ui/g-web/ {
                proxy_pass http://myservice;
                proxy_read_timeout 300;
                proxy_connect_timeout 300;

                proxy_set_header   X-Forwarded-Proto $scheme;
                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;
            }
            # 静态资源映射
            location /static/resource {
                alias /usr/app/static/resource/;
            }

            # 内网deny 
            location /path/api/inner {
                   deny all;
            }
            
            location /path/inner {
                   deny all;
            }
            
            location /path/ui {
                   deny all;
            }
    }

    # 对内
    server {
            listen 8000;
            listen [::]:8000;

            #root /var/www/html;

            # Add index.php to the list if you are using PHP
            #index index.html index.htm index.nginx-debian.html;


            location /path/ {
                proxy_pass http://myservice;
                proxy_read_timeout 300;
                proxy_connect_timeout 300;

                proxy_set_header   X-Forwarded-Proto $scheme;
                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;
            }
    }

}
View Code

 

3、几个值得注意的点:


 

(1)Nginx内网配置通过VIP访问8000端口,实现web内网访问控制。

(2)Nginx外网配置监听80端口,由DNS 或者 F5等切入流量。

(3)虚拟IP(Vrtual IP Address),是一种不与特定计算机或者特定计算机网卡相对应的IP地址。所有发往这个IP地址的数据包最后都会经过真实的网卡到达目的主机的目的进程。虚拟IP比较常见的一个用例就是在系统高可用性(High Availability HA)方面的应用,通常一个系统会因为日常维护或者非计划外的情况而发生宕机,为了提高系统对外服务的高可用性,就会采用主备模式进行高可用性的配置。当提供服务的主机M宕机后,服务会切换到备用主机S继续对外提供服务。而这一切用户是感觉不到的,在这种情况下系统对客户端提供服务的IP地址就会是一个虚拟IP,当主机M宕机后,虚拟IP便会漂浮到备机上,继续提供服务。

 

posted on 2020-01-07 14:51  小猩  阅读(3023)  评论(0编辑  收藏  举报

导航