下载Nginx链接 http://nginx.org/en/download.html
我这里是基于 nginx-1.18.0 版本进行安装演示的,演示IP 192.168.106.139。
# cd / # mkdir software # cd /software/ # ll total 1016 -rw-r--r-- 1 root root 1039530 Oct 22 10:19 nginx-1.18.0.tar.gz
# cd /software/ # tar -zxvf nginx-1.18.0.tar.gz -C /usr/local/ # cd /usr/local/ # ll total 18964 drwxr-xr-x. 2 root root 49 Apr 15 2021 bin drwxr-xr-x. 2 root root 6 Apr 11 2018 etc drwxr-xr-x. 2 root root 6 Apr 11 2018 games drwxr-xr-x. 2 root root 6 Apr 11 2018 include drwxr-xr-x. 2 root root 6 Apr 11 2018 lib drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64 drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec drwxr-xr-x 8 1001 dba 158 Apr 21 2020 nginx-1.18.0 drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin drwxr-xr-x. 5 root root 49 Mar 25 2021 share drwxr-xr-x. 2 root root 6 Apr 11 2018 src
压缩包已解压完成。
# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
PCRE 的作用是让 Nginx 支持 Rewrite 功能。
http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
下载完成后将文件上传至我们之前创建好的 /software 目录下,然后解压到 /usr/local 目录下
# cd /software # ll total 2968 -rw-r--r-- 1 root root 1039530 Oct 22 10:19 nginx-1.18.0.tar.gz -rw-r--r-- 1 root root 1996552 Oct 22 10:38 pcre-8.35.tar.gz # tar -zxvf pcre-8.35.tar.gz -C /usr/local/ # cd /usr/local/ # ll | grep pcre drwxr-xr-x 7 1169 1169 8192 Apr 4 2014 pcre-8.35
# cd pcre-8.35 # ./configure # make && make install
# pcre-config --version 8.35
# cd /usr/local/nginx-1.18.0 # ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35 # make # make install
# /usr/local/webserver/nginx/sbin/nginx -v nginx version: nginx/1.18.0
到此,Nginx 安装完成。
配置nginx.conf ,将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容
worker_processes 1; #设置值和CPU核心数一致 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别 pid /usr/local/webserver/nginx/nginx.pid; #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; upstream active_mq{ server 192.168.106.136:8161; server 192.168.106.137:8161; } sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # 代理 activeMQ 控制台页面 server { listen 8161; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://active_mq; } } # another virtual host using mix of IP-, name-, and port-based configuration #下面是server虚拟主机的配置 server{ listen 80;#监听端口 server_name localhost;#域名,名称可以自己定义 index index.html index.htm index.php; root /usr/local/webserver/nginx/html; #站点目录 location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 15d; } access_log off; } # 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 配置文件的正确性:
# /usr/local/webserver/nginx/sbin/nginx -t nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
启动 nginx 服务,查看进程
# /usr/local/webserver/nginx/sbin/nginx # ps -ef | grep nginx
使用浏览器访问我们配置的站点
在上一章节中我们配置了 ActiveMQ 的8161端口,它属于 http 协议,用于访问 ActiveMQ 的控制台,那如何使我们安装的 Nginx 能够代理 tcp 协议的端口呢?比如 ActiveMQ 的 61616 端口。
Nginx 中提供了 stream 模块,可以用来代理 tcp 协议的端口,安装 Nginx 的时候默认没有加载 stream 模块。
所以在 nginx.conf 文件中配置了 stream,在检查配置文件正确性时会报错。
stream{ #-----------------------ActiveMQ----------------------# upstream active_mq_61616{ server 192.168.106.136:61616; server 192.168.106.137:61616; } server{ listen 61616; proxy_pass active_mq_61616; proxy_connect_timeout 10s; proxy_timeout 300s; } }
# /usr/local/webserver/nginx/sbin/nginx -t nginx: [emerg] unknown directive "stream" in /usr/local/webserver/nginx/conf/nginx.conf:104 nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test failed
检查不通过
为了解决这个问题,我们需要重新对源文件进行编译、安装,通过添加–with-stream参数指定安装stream模块。
进入 /usr/local/nginx-1.18.0 目录
# cd /usr/local/nginx-1.18.0 # ./configure --with-stream 或者 ./configure --prefix=/usr/local/webserver/nginx --with-stream # make & make install
检查配置文件正确性
/usr/local/webserver/nginx/sbin/nginx -t
文件检查通过,说明 stream 模块安装成功。
重启 Nginx,验证 61616 端口是否可用。
window中打开 cmd 命令行,输入如下命令:
telnet 192.168.106.139 61616
经过验证,Nginx 代理 tcp 协议配置完成。