Linux安装nginx配置反向代理

13 篇文章 0 订阅

🐔安装nginx

🐥Linux安装nginx

官网下载: https://nginx.org/
我这里自己下载的是稳定版本 nginx-1.24.0

1️⃣上传到对应目录

cd /usr/local
mkdir nginx
cd /usr/local/nginx
# 上传命令
rz
# 没有话安装一下
yum install lrzsz

2️⃣解压nginx

tar -zxvf nginx-1.24.0.tar.gz 
# 一样没有就安装一下
yum install tar
#nginx基于c语言开发,因此需要先安装c语言编译环境以及相关依赖。
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
# 进入文件夹
cd nginx-1.24.0
# 执行configure可执行文件
./configure
# 编译并安装
make && make install
cd /usr/local/nginx/sbin
# 执行nginx可执行文件,启动nginx服务
./nginx
# 停止nginx服务
./nginx -s stop
# 重启nginx服务
./nginx -s reload

3️⃣检查是否启动成功

ps -ef|grep nginx

在这里插入图片描述
在这里插入图片描述

出现这个两个页面说明启动成功了

🐥使用docker安装nginx

怎么安装docker? 看下面这篇就够了
https://blog.csdn.net/qq_39017153/article/details/131955100

1️⃣拉取镜像

docker pull nginx:latest

2️⃣启动镜像

docker run --name nginx \
-p 80:80 \
-p 443:443 \
-v /data/apps/nginx/html:/usr/share/nginx/html \
-v /data/apps/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /data/apps/nginx/logs:/var/log/nginx/ \
-v /data/apps/nginx/ssl:/etc/nginx/ssl/ \
--privileged=true -d --restart=always nginx

3️⃣查看镜像是否启动成功

docker ps

🐥nginx基本配置

1️⃣简单的反向代理

server {
    listen 80;                                #监听端口
    server_name localhost;                    #服务器名称
    location / {                              #匹配请求url地址
        root html;                            #指定返回静态资源路径
        index index.html;                     #默认首页
        
        # 设置请求头参数
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8080;     #反向代理配置,将请求转发到指定服务器
    }
}
server {
    listen 80;
    server_name localhost;

    gzip on;
    gzip_static on;     # 需要http_gzip_static_module 模块
    gzip_min_length 1k;
    gzip_comp_level 4;
    gzip_proxied any;
    gzip_types text/plain text/xml text/css;
    gzip_vary on;
    gzip_http_version   1.0; #兼容多层nginx 反代
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    # 前端打包好的dist目录文件
    root /data/dist/;

    location ^~/api/ {
        proxy_pass http://后端服务地址/; #注意/后缀
        proxy_connect_timeout 60s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto http;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
    }

    # 避免端点安全问题
    if ($request_uri ~ "/actuator"){
	    return 403;
    }
}

proxy_pass配置反向代理,意味当80端口被访问时,会将请求转发到http://127.0.0.1:8080对应服务上。

2️⃣介绍location配置中root和alias的区别

在这里插入图片描述

  • 使用 root 时, 服务器里真实的资源路径是 root 的路径拼接上 location 指定的路径
  • 使用alias顾名思义是代指 location 的别名, 不论location 是什么, 资源的真实路径都是alias所指定的,所以location是匹配浏览器输入的地址, 真实访问的路径就是alias 指定的路径
  • 其他区别
    1. alias 只能配置在location 中, 而root 可以配置在 server, http 和 location 中
    2. alias 后面必须要以 “/” 结尾, 否则会查找不到文件, 报404错误; 而 root 对 “/” 可有可无

如果是vue项目 项目刷新以后变成404的问题 try_files $uri $uri/ /popularize/index.html; 加上这段配置即可

    location /dist{
            root  /opt/vue/star-honey-popularize-manage;
            index  index.html index.htm;
            # 解决vue项目刷新以后变成404的问题
           try_files $uri $uri/ /dist/index.html;
        }

3️⃣nginx 配置https证书

	 server {
        listen       443 ssl;
        server_name  www.abc.com;#你们的域名,如www.abc.com;

        ssl_certificate      ssl/public.pem; #根据实际的路径和文件名配置,我这里配置的是相对路径,相对路径前面不能跟/
        ssl_certificate_key  ssl/private.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;
        }
    }

3️⃣把http 转发到https

   server {
        listen 80;
        #需要将clover.fit替换成证书绑定的域名。
        server_name www.abc.com; #需要转发的域名,如www.abc.com;
        #将所有HTTP请求通过rewrite指令重定向到HTTPS。
        rewrite ^(.*)$ https://$host$1; 
        location / {
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }

🐥nginx遇到错误

1️⃣NGINX 上的 SSL 证书无法加载

nginx: [emerg] cannot load certificate "/etc/nginx/public.pem": BIO_new_file() failed (SSL: error:80000002:system library::No such file or directory:calling fopen(/etc/nginx/public.pem, r) error:10000080:BIO routines::no such file)
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

没有权限读取

路径配置错误或docker服务ssl目录未挂载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值