美文网首页工作生活
nginx安装及常用配置

nginx安装及常用配置

作者: 老菜根 | 来源:发表于2019-07-08 14:31 被阅读0次

下载nginx

cd /opt
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0/

安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装nginx

cd /opt/nginx-1.16.0/
./configure
make && make install

这里./configure没有添加任何参数,以默认配置编译安装。
默认安装后nginx目录为:/usr/local/nginx/
nginx执行文件路径为:/usr/local/nginx/sbin/nginx
nginx配置文件路径为:/usr/local/nginx/conf/nginx.conf
如果需要修改配置则可以查看【官方文档】nginx building configure命令支持参数

nginx常用命令

./nginx  #启动nginx
./nginx -v #查看nginx版本
./nginx -V #查看nginx版本详细信息
./nginx -s stop #停止nginx
./nginx -s quit  #停止nginx
./nginx -s reload #重载nginx配置
./nginx -c /usr/local/nginx/conf/nginx.conf  指定配置文件
./nginx -t  #检查配置文件是否正确
./nginx -h  #显示帮助信息

其中:

  • -s都是采用向 Nginx 发送信号的方式
  • -c表示configuration,指定配置文件

添加模块,使nginx支持更多功能

1.nginx同时正向代理http和https

默认的情况下,使用nginx做正向代理可以解析http请求,但不支持https请求,可以借助第三方模块ngx_http_proxy_connect_module实现

1.首先确保安装了patch,gcc、gcc++、pcre、zlib,这些编译时需要用到的依赖软件或静态库。

yum group install -y "Development Tools"
yum install -y patch pcre-devel pcre zlib-devel zlib

2.下载ngx_http_proxy_connect_module第三方模块

cd /opt
git clone https://github.com/chobits/ngx_http_proxy_connect_module

3.重新编译安装nginx

cd nginx-1.16.0/
# patch -p1 < {当前nginx版本对应proxy_connect.patch路径},其他版本可查看模块主页上的文档找到对应版本的patch
patch -p1 < /opt/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch
./configure --prefix=/usr/local/nginx --add-module=/opt/ngx_http_proxy_connect_module
make && make install

除了make && make install这种方式以外,也可以直接替换nginx执行文件

cd /usr/local/nginx/sbin/
mv nginx nginx-back
cp /opt/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/nginx

4.修改配置文件

vi /usr/local/nginx/conf/nginx.conf

正向代理示例配置如下:

 server {
     listen                         3128;

     # dns resolver used by forward proxying
     resolver                       8.8.8.8;

     # forward proxy for CONNECT request
     proxy_connect;
     proxy_connect_allow            443 563;
     proxy_connect_connect_timeout  10s;
     proxy_connect_read_timeout     10s;
     proxy_connect_send_timeout     10s;

     # forward proxy for non-CONNECT request
     location / {
         proxy_pass http://$host;
         proxy_set_header Host $host;
     }
 }

2.nginx配置邮件代理

这个主要用于当前主机无法访问外网,而借助其他可以访问外网的内网主机进行邮件处理。

1.编译安装nginx

cp /opt/nginx-1.16.0/
./configure --prefix=/usr/local/nginx --with-mail --with-stream
make && make install

2.修改配置文件

vi /usr/local/nginx/conf/nginx.conf

邮件代理示例配置如下,注意:以下配置与events、http同级

stream{
    server {
        listen       25;
        proxy_pass smtp.qq.com:465;
    }
}

配置完成,则应用服务器发邮件的配置中,邮件发送的服务器地址填写nginx服务器的地址,端口写邮件代理配置上监听的端口。

3.nginx配置反向代理

反向代理示例:

    server {
        listen       80;
        server_name  localhost;

        location / {
           proxy_pass http://localhost:8080;
           proxy_redirect default;
        }
    }

rewrite形式的反向代理示例:

    server {
        listen       80;
        server_name  localhost;

        location ^~ /api {
                rewrite ^/api/(.*)$ /$1 break;
                proxy_pass http://127.0.0.1:8080;
                proxy_intercept_errors on;
        }
    }

4.http代理负载均衡

使用proxy_pass指定负载均衡方式,示例如下:

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server 192.0.0.1 backup;
    }
    
    server {
        location / {
            proxy_pass http://backend;
        }
    }
}

相关文章

网友评论

    本文标题:nginx安装及常用配置

    本文链接:https://www.haomeiwen.com/subject/qjarhctx.html