Nginx linux安装及配置



文章目录

  • Nginx linux安装及配置
  • 一、介绍
  • 1.1 概述
  • 1.2 主要功能:
  • 1.2.1 正向代理
  • 1.2.2 反向代理
  • 1.2.3 负载均衡
  • 1.2.4 动静分离
  • 二、准备
  • 2.1 更新系统软件
  • 2.2 安装依赖
  • 2.3 查询安装的软件
  • 2.4 下载nginx
  • 方法1、手动上传(离线上传)
  • 方法2、在线下载
  • 三、安装nginx
  • 3.1 解压
  • 3.2 创建makeFile文件
  • 3.3 开始编译
  • 3.4 安装nginx
  • 3.5 查看模块(可选)
  • 四、配置nginx.conf
  • 五、相关命令
  • 六、常见问题
  • 6.1 make时报错make: *** No targets specified and no makefile found. Stop.
  • 6.2 make 时报错 ./configure: error: *** library


一、介绍

1.1 概述

nginx是一款高性能的HTTP反向代理服务器。

优点

  • 支持高并发连接:官方测试Nginx能够支撑5万并发连接,实际生产环境中可以 支撑2~4万并发连接数;
  • 配置文件简洁;
  • 占用内存少;
  • 内部有健康检查功能;
  • 支持Rewrite重写:可根据不同的域名、URL将请求转发至后端不同的服务群;
  • 支持热部署;
  • 稳定性高。

1.2 主要功能:

正向代理与反向代理;负载均衡;动静分离;

1.2.1 正向代理

nginx可以做正向代理进行上网等功能。加入将局域网外的网络想象成是一个大的资源库,如果想要访问局域网外的资源库,则需要通过代理服务器来访问外部资源。

1.2.2 反向代理

反向代理,其实客户端对反向代理服务器是无感的,客户端访问反向代理服务器,反向代理服务器再将请求转发至目标服务器,只需要暴露代理服务器的地址即可,隐藏了真实的服务器地址。

1.2.3 负载均衡

对于多个请求,nginx将请求分发至不同的服务器上,从而来减轻服务器压力。

1.2.4 动静分离

为了加快网站的解析速度,可以把动态资源(jsp、servlet)和静态资源(js、HTML等)由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。

二、准备

准备

2.1 更新系统软件

yum update

2.2 安装依赖

需提前安装gcc的环境

# make命令需要gcc
yum install gcc gcc-c++ autoconf automake

第三方的开发包

  • PCREPerl Compatible Regular Expressions 是一个Perl库,包括 perl 兼容的正则表达式库。nginxhttp 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。pcre-devel是使用pcre开发的一个二次开发库。nginx需要
  • zlib:库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
  • OpenSSLnginx 不仅支持 http 协议,还支持 https ,即在ssl协议上传输http,所以需要在Linux安装openssl库
# 安装依赖pcre、zlib和openssl
yun install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.3 查询安装的软件

rpm -ql pcre

2.4 下载nginx

方法1、手动上传(离线上传)

nginx 官方下载地址:下载地址

下载完,自行通过 ftp工具传上去。我这里放在/home下面

方法2、在线下载
# 下载最新nginx-1.20.1
curl -O http://nginx.org/download/nginx-1.20.1.tar.gz
或者
wget http://nginx.org/download/nginx-1.20.1.tar.gz
# 如果没有wget,执行yum

三、安装nginx

3.1 解压

进入/home目录

[root@muzi home]# tar -zxvf nginx-1.20.1.tar.gz

[root@muzi home]# cd /usr/local/nginx-1.20.1
[root@muzi nginx-1.20.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

3.2 创建makeFile文件

注意这里很多人犯错,因为当前目录没有 Makefile 文件,所以不能直接进行 make 编译,会提示 make: *** No targets specified and no makefile found. Stop.

需要使用 configure 命令创建一个 Makefile 文件。也可以一步到位,使用命令 ./configure && make 创建并编译

[root@muzi nginx-1.20.1]# ./configure

执行后可以看到Makefile文件

creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

3.3 开始编译

[root@muzi nginx-1.20.1]# make

# make 成功后,此时当前目录会出现 objs 目录,objs 目录中会生成有 nginx 文件
[root@muzi nginx-1.20.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

3.4 安装nginx

# 安装nginx,可以将启动脚本注册成全局,默认安装在 /usr/local 目录下
[root@muzi nginx-1.20.1]# make install
[root@muzi nginx]# ls /usr/local/nginx/
conf  html  logs  sbin

# conf:配置文件夹
# html:静态资源文件
# sbin:启动文件
# logs:日志文件

# 如果之前已经安装nginx的,这里就不再make install,否则会覆盖掉之前的安装和配置

3.5 查看模块(可选)

[root@muzi nginx]# ./sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
configure arguments:

如果需要配置 https ,参考另一篇文档。

四、配置nginx.conf

nginx.conf 配置文件的相关配置介绍:

配置开始,进入到 /usr/local/nginx-1.20.1/conf ,查看默认配置文件内容

[root@muzi nginx-1.20.1]# cat ./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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

修改 nginx.conf ,简单配置如下:

[root@muzi nginx-1.20.1]# cat ./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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   html;
        }

    }

}

本人项目完整配置

[root@muzi nginx]# cat ./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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        #location / {
            #return 301 https://127.0.0.1$request_uri;
            #root   html;
            #index  index.html index.htm;
        #}
        location / {
            root   /home/testMerge/dist/;  
            index  index.html;
            client_max_body_size 100m;
            client_body_buffer_size 512k;
            proxy_send_timeout 300;
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_buffer_size 64k;
            proxy_buffers 16 64k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
        }
		
        location /api/ {
            proxy_pass http://127.0.0.1:11000/;
            client_max_body_size 100m;
            client_body_buffer_size 512k;
            proxy_send_timeout 300;
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_buffer_size 64k;
            proxy_buffers 16 64k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
        }

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

    }

}

五、相关命令

# 指定配置文件启动nginx
./sbin/nginx -c ./conf/nginx.conf

# 关闭nginx进程
nginx -s stop

# 重新加载配置文件,就是加载上面的Makefile
nginx –s reload

# 检查配置文件是否有误(很重要)
nginx –t

# 保存配置并退出:
./nginx -s quit

# 查找安装路径
whereis nginx

六、常见问题

6.1 make时报错make: *** No targets specified and no makefile found. Stop.

解决办法:如果这是因为没有找到 makefile 。因为是源码安装,本地解压后是没有 makefile 文件的,先运行 ./configure,生成 makefile ,再执行 make ,即可正常运行。

6.2 make 时报错 ./configure: error: *** library

多半是因为缺少依赖,安装即可。

例如以下错误:

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.