下载Nginx

Nginx官网,在这里我们下载到我们需要的版本,我们选择最新的版本下载。我们点击documentation -> installing nginx, 找到Installation on Linux的文档,然后我们按文档进行操作。

通过命令查询系统的platform

cat /etc/*-release

然后找到对应系统platform的文档,按文档进行安装即可。我这边系统是CentOS,找到对应的操作。

RHEL and derivatives This section applies to Red Hat Enterprise Linux and its derivatives such as CentOS, Oracle Linux, Rocky Linux, AlmaLinux.

我这边没有选择这个,我直接选择从源码安装,首先下载资源,我们现在Stable version(稳定版本)https://nginx.org/en/download.html

wget https://nginx.org/download/nginx-1.24.0.tar.gz

复制链接地址,在服务器上使用wget下载,然后解压

tar -xzf nginx-1.24.0.tar.gz

从源码安装

使用下面命令先配置

./configure \
    --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module 

出现如下说明配置成功配置成功

Configuration summary
  + using system PCRE2 library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx"
  nginx configuration file: "/usr/local/nginx/nginx.conf"
  nginx pid file: "/usr/local/nginx/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"

可能会出现下面的错误

./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using --with-openssl=<path> option.

是因为没有安装openssl,使用下面命令安装,

sudo yum install openssl-devel

安装后我们需要重新执行上面的 ./configure 的命令

接着我们开始编译(compiled and installed)

make && make install

出现下面的类似提示说明编译成功

objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre2-8 -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/home/nginx/nginx-1.24.0'
...
...
...
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/home/nginx/nginx-1.24.0'

启动Nginx

我们接着看官方的文档,如何启动nginx ,初学者指南,这里可以看到基本的操作。进入到 /usr/local/nginx 中执行 ./nginx 则启动了nginx,浏览器访问对应ip,则会出现下面的页面,则启动成功

更多的命令可以查看文档。

配置Nginx

更多配置可以看文档,我们这里简单的配置一个反向代理,访问我们的springboot启动的一个web服务,端口是8080

upstream reading {
       server 127.0.0.1:8080;
       #server 127.0.0.1:9001;
}
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://reading;
            proxy_set_header Host $host;
            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;
        }
        location /html {
            root   html;
            index  index.html index.htm;
        }
}

上面的配置在http模块里面进行,报错后使用 ./nginx -s reload 重启就生效了,页面直接输入ip(如果服务器绑定了域名,server_name使用域名,则可以通过域名直接访问到对应的后端服务),则可以直接访问到服务器上的8080服务

到这里nginx的基本安装、启动、配置已经完成了,更多配置和功能可以查看官方文档。