合 Nginx安装配置及端口转发
简介
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。
在我们实际开发应用中,需要了解Nginx的以下几个功能
- 正向代理
- 反向代理
- 负载均衡
- 动静分离
- 端口转发
域名简介
域名就是网站:www.xmmup.com就是域名
DNS域名解析服务器,把域名解析为ip地址。保存的就是域名和ip地址的映射关系。
一级域名:xmmup.com
二级域名:www.xmmup.com
三级域名:pic.xmmup.com
一个域名对应与一个ip地址,一个ip地址可以被多个域名绑定。
只需要买一个一级域名,后面的二级,三级域名你自己可以随便定义。
下载
http://nginx.org/en/download.html
官网提供三种版本:
Nginx官网提供了三个类型的版本
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版
Linux环境安装
由于nginx是基于c语言开发的,所以需要安装c语言的编译环境。
yum安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install -y nginx* nginx-mod-devel nginx-mod-stream nginx-all-modules systemctl enable nginx systemctl start nginx systemctl status nginx -- 默认配置文件 /etc/nginx/nginx.conf -- 日志 /var/log/nginx/error.log [root@lhrxxt ~]# netstat -tulnp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23092/nginx: master tcp6 0 0 :::80 :::* LISTEN 23092/nginx: master [root@lhrxxt ~]# [root@lhrxxt ~]# nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.1.1k FIPS 25 Mar 2021 TLS SNI support enabled configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' [root@lhrxxt ~]# |
1、Linux release 7.6.1810 会安装nginx-1.20.1版本。
2、默认安装了端口转发模块--with-stream=dynamic
编译安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | yum -y install gcc pcre pcre-devel zlib-devel openssl openssl-devel nginx-all-modules cd /soft/ wget http://nginx.org/download/nginx-1.22.1.tar.gz tar -zxvf nginx-1.22.1.tar.gz cd nginx-1.22.1 ./configure --prefix=/usr/local/nginx --with-mail --with-stream --with-stream=dynamic --with-stream_ssl_module --with-http_v2_module --with-stream_realip_module make -j8 make install echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile source /etc/profile -- 启动 nginx -t nginx -c /usr/local/nginx/conf/nginx.conf -- 或者使用nginx.service -- 日常生产环境使用nginx,编译模块按照nginx官方yum安装的模块,基本能满足95%以上的生产需求。 [root@test nginx-1.22.1]# ll /usr/local/nginx/modules/ total 1224 -rwxr-xr-x 1 root root 1249688 Feb 8 10:38 ngx_stream_module.so [root@test nginx-1.22.1]# cd .. [root@test soft]# cd /usr/local/nginx/ [root@test nginx]# ll total 20 drwxr-xr-x 2 root root 4096 Feb 8 10:38 conf drwxr-xr-x 2 root root 4096 Feb 8 10:38 html drwxr-xr-x 2 root root 4096 Feb 8 10:38 logs drwxr-xr-x 2 root root 4096 Feb 8 10:38 modules drwxr-xr-x 2 root root 4096 Feb 8 10:38 sbin [root@test nginx]# |
--with-stream 表示支持tcp和udp端口转发。
--with-stream=dynamic
与 --with-stream
的区别在于,--with-stream=dynamic
可以在 nginx 的配置文件配置动态加载模块,不需要使用该模块时可以不加载。
nginx配置systemd的service服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | cat > /usr/lib/systemd/system/nginx.service <<"EOF" [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl start nginx #启动nginx服务 systemctl status nginx #查看nginx服务状态 systemctl stop nginx #停止nginx服务 |
模块的加载
模块的加载在 nginx 的配置文件中可以配置,在配置文件中的开头部分使用 load_module 加载模块:
1 | load_module /usr/local/nginx/modules/ngx_stream_module.so; |
模块加载也可以单独放在一个或多个文件中,然后通过 include 引入到 nginx.conf 中