方式1 yum直接安裝
- 安裝nginx
yum install nginx
- 安裝模塊
yum install nginx-mod-stream -y
- 添加轉(zhuǎn)發(fā)配置 /etc/nginx/nginx.conf底部添加
stream {
server {
listen 8666;
proxy_connect_timeout 360s;
proxy_timeout 360s;
proxy_pass xxx.xxx.xxx.xxx:8666;
}
}
- 運(yùn)行測試
# 運(yùn)行
nginx
# 重載配置文件
nginx -s reload
方式2 源碼編譯
一、安裝編譯工具
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、安裝PCRE
PCRE (Perl Compatible Regular Expressions) 是一個用于處理正則表達(dá)式的庫岂傲,它是一個C語言的庫礁蔗,可以在多種編程語言中使用鳖眼。PCRE庫提供了一套API來編譯和執(zhí)行正則表達(dá)式特铝,并提供了一組函數(shù)來匹配、查找和替換文本中符合正則表達(dá)式的模式棍郎。PCRE庫與Perl的正則表達(dá)式語法兼容其障,因此可以直接使用Perl的正則表達(dá)式語法。
如果我們在nginx.conf配置文件中使用了正則表達(dá)式涂佃,那么在編譯Nginx時必須將PCRE庫編譯進(jìn)Nginx励翼。這是因?yàn)镹ginx的HTTP模塊需要依賴PCRE庫來解析正則表達(dá)式。
- 進(jìn)入下載目錄
cd /usr/local/src/
- 下載
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
- 解壓
tar zxvf pcre-8.35.tar.gz
- 安裝
./configure
make && make install
三辜荠、編譯運(yùn)行nginx
- 下載源碼
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.25.3.tar.gz
- 解壓
tar zxvf nginx-1.25.3.tar.gz
- 進(jìn)入源碼目錄
cd nginx-1.25.3
- 配置
./configure --prefix=/opt/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-pcre=/usr/local/src/pcre-8.35 --with-stream
- 編譯安裝
# 編譯
make
# 安裝
make install
- 配置文件 /opt/nginx/etc/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
#pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
include mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /opt/nginx/conf/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /opt/nginx/conf/default.d/*.conf;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
include /opt/nginx/conf/vhosts/*.conf;
}
stream {
server {
listen 8666;
proxy_connect_timeout 360s;
proxy_timeout 360s;
proxy_pass locktcp.cosinwx.com:8666;
}
}
- 運(yùn)行
# 運(yùn)行
./opt/nginx/sbin/nginx
# 重載配置文件
./opt/nginx/sbin/nginx -s reload