反向代理——Nginx(学习笔记)

Nginx是一款高性能的Web服务器,也是一个反向代理服务器,常用于静态资源的代理、负载均衡、反向代理、缓存等。下面是Nginx的安装和使用方法。

 安装Nginx

1. 下载安装包:在官网下载最新版本的Nginx安装包,解压到指定目录,假设解压到/usr/local/src目录下。

cd /usr/local/src
wget http://nginx.org/download/nginx-1.21.3.tar.gz
tar zxvf nginx-1.21.3.tar.gz

2. 安装依赖:安装编译Nginx所需的依赖库。

yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel

3. 编译安装:进入解压后的目录,执行以下命令进行编译安装:

cd nginx-1.21.3
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-zlib
make && make install

4. 启动Nginx:执行以下命令启动Nginx:

/usr/local/nginx/sbin/nginx

4. 检查Nginx是否启动成功:在浏览器中输入服务器IP地址,如果出现“Welcome to Nginx”字样,则说明Nginx已经成功启动。

配置Nginx

1. 修改配置文件:Nginx的配置文件位于`/usr/local/nginx/conf/nginx.conf`,可以根据需要进行修改。其中,`listen`后面带着的是监听某个端口,`server_name`填的是你自己的域名。

server {
    listen 80;
    server_name example.com;

    location / {
        root /path/to/your/web/root;
        index index.html;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_fo…[omitted]
    }
}

2. 静态文件代理:在`http`块中添加以下代码,指定前端静态文件的位置:

http {
    ...

    server {
        ...

        location / {
            root /path/to/your/web/root;
            index index.html;
        }
    }

    ...
}

3. 反向代理:在`http`块中添加以下代码,解决前后端跨域问题:

http {
    ...

    server {
        ...

        location /api/ {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_fo…[omitted]
        }
    }

    ...
}

4. 重启Nginx:修改完配置文件后,执行以下命令重启Nginx:

cd /usr/local/nginx/sbin
./nginx -s reload

Nginx实例

 可以将Nginx配置文件分为三个部分:全局配置、HTTP配置和服务器配置。

全局配置

在全局配置部分,我们可以设置Nginx的一些全局参数,如用户、工作进程数和错误日志路径等。以下是全局配置的注释说明:

user  nobody;  # 运行Nginx的用户
worker_processes  1;  # 工作进程数

error_log  logs/error.log;  # 错误日志路径
pid        logs/nginx.pid;  # Nginx进程ID存储位置

events {  # 事件模型配置
    worker_connections  1024;  # 每个工作进程的最大连接数
}


HTTP配置

在HTTP配置部分,我们可以设置HTTP相关的参数,如MIME类型、默认类型、keepalive超时时间等。以下是HTTP配置的注释说明:

http {  # HTTP配置
    include       mime.types;  # MIME类型映射文件路径
    default_type  application/octet-stream;  # 默认MIME类型

    sendfile        on;  # 开启sendfile技术
    tcp_nopush     on;  # 开启tcp_nopush技术

    keepalive_timeout  65;  # keepalive超时时间

    gzip  on;  # 开启gzip压缩
}

服务器配置


在服务器配置部分,我们可以设置具体的虚拟主机和相关的路由规则。以下是服务器配置的注释说明:


server {  # 虚拟主机配置
    listen       80;  # 监听端口
    server_name  localhost;  # 主机名

    location / {  # 根路径配置
        root   html;  # 根目录
        index  index.html index.htm;  # 默认文件
    }

    error_page  404              /404.html;  # 错误页面配置

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;  # 错误页面重定向
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    location ~ .php$ {  # PHP脚本代理配置
       proxy_pass   http://127.0.0.1;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ .php$ {  # PHP脚本FastCGI配置
       root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
       include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root concurs with nginx's one
    location ~ /.ht {  # 阻止访问.htaccess文件
       deny  all;
    }
}


这些服务器配置包括监听端口、根路径、错误页面、代理和FastCGI等规则。

一个完整的Nginx完整配置文件

user  nobody;  # 运行Nginx的用户
worker_processes  1;  # 工作进程数

error_log  logs/error.log;  # 错误日志路径
pid        logs/nginx.pid;  # Nginx进程ID存储位置

events {  # 事件模型配置
    worker_connections  1024;  # 每个工作进程的最大连接数
}

http {  # HTTP配置
    include       mime.types;  # MIME类型映射文件路径
    default_type  application/octet-stream;  # 默认MIME类型

    sendfile        on;  # 开启sendfile技术
    tcp_nopush     on;  # 开启tcp_nopush技术

    keepalive_timeout  65;  # keepalive超时时间

    gzip  on;  # 开启gzip压缩

    server {  # 虚拟主机配置
        listen       80;  # 监听端口
        server_name  localhost;  # 主机名

        location / {  # 根路径配置
            root   html;  # 根目录
            index  index.html index.htm;  # 默认文件
        }

        error_page  404              /404.html;  # 错误页面配置

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;  # 错误页面重定向
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        location ~ .php$ {  # PHP脚本代理配置
           proxy_pass   http://127.0.0.1;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ .php$ {  # PHP脚本FastCGI配置
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root concurs with nginx's one
        location ~ /.ht {  # 防止访问.htaccess文件
           deny  all;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值