Nginx请求转发到后端服务器/K8S/PaaS

795 阅读1分钟

Nginx安装及配置

Nginx安装

  1. https://nginx.org/en/download.html下载nginx
   解压:tar –xzf nginx-1.10.3.tar.gz && cd nginx-1.10.3
   配置:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
   编译安装 sudo make && sudo make install
   启动:/usr/local/nginx/sbin/nginx
   查看进程:ps –aux | grep nginx
   验证 curl http://127.0.0.1/
  1. 如果安装时提示缺少compiler cc,需要安装gcc,可以从网址http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/下载以下包:
mpfr-3.1.1-4.el7.x86_64.rpm
libmpc-1.0.1-3.el7.x86_64.rpm
kernel-headers-3.10.0-123.el7.x86_64.rpm
glibc-headers-2.17-55.el7.x86_64.rpm
glibc-devel-2.17-55.el7.x86_64.rpm
cpp-4.8.2-16.el7.x86_64.rpm
gcc-4.8.2-16.el7.x86_64.rpm
  1. 然后安装:rpm -Uvh *.rpm --nodeps --force
  2. 如果安装时提示./configure: error: the HTTP rewrite module requires the PCRE library.
  3. 安装openssl即可:yum -y install openssl openssl-devel
  4. 如果提示需要PCRE包:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
  1. 需要进行安装:yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

配置Nginx转发到后端服务器/K8S/PaaS

worker_processes  1;
user root; # 如果nginx没有权限写入到临时文件目录下可能会抛出异常,需要设置为root用户,或在目录下给nginx授权。

events {
    worker_connections  1024;
}

http {

    include       mime.types;

    default_type  application/octet-stream;
    
    # 以下几个字段为文件相关的,如果遇到413 Request Entity Too Large或500时需要配置
    # nginx的异常可查看error.log进行定位
    client_max_body_size 1000m; # 上传文件的最大值
    client_body_buffer_size 100m; # 请求缓存区的大小
    client_body_temp_path /tmp; # 临时文件存放目录,只有当上传的请求体超出缓存区大小时,才会写到临时文件中。

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {
            # nginx的转发配置
            proxy_pass {PaaS应用的域名};

            proxy_redirect off;

        }
    }
}