以下在CentOS系统下安装
1、安装编译工具和库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、nginx的rewrite伪静态匹配规则用到正则,通过pcre实现,所以需要安装pcre
mkdir -p /app/pcre
cd /app/pcre
wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz
tar zxvf pcre-8.45.tar.gz
cd pcre-8.45
./configure --prefix=/app/pcre
make && make install
pcre-config --version
3、安装和启用nginx
mkdir /app
chmod 777 /app
mkdir -p /app/nginx
cd /app/nginx
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-pcre=/app/pcre/pcre-8.45
make && make install
/app/nginx/sbin/nginx
/app/nginx/sbin/nginx -v
/app/nginx/sbin/nginx -s reload
4、配置文件
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80; ##监听端口
server_name localhost;
location / { ###访问的路径,如http://127.0.0.1/index.html
root html; ###html文件所在目录
index index.html index.htm; ###html文件名
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
代理配置例子
1、四层代理
stream {
upstream ssh {
server 192.168.211.129:22;
}
server {
listen 8080;
proxy_pass ssh;
proxy_connect_timeout 1h;
proxy_timeout 1h;
}
}
2、web应用代理
http {
server {
listen 80; ##监听端口
server_name localhost;
location / { ###访问的路径,如http://127.0.0.1/index.html
root html; ###html文件所在目录
index index.html index.htm; ###html文件名
}
}
proxy_pass路径说明,proxy_pass 代理规则(是否以“/”结尾)
例子1、如下配置
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
当访问 http://127.0.0.1/proxy/test.html,最终代理到 URL 是 http://127.0.0.1/test.html
例子2、如下配置
location /proxy/ {
proxy_pass http://127.0.0.1;
}
当访问 http://127.0.0.1/proxy/test.html,最终代理到 URL 是 http://127.0.0.1/proxy/test.html