# Nginx学习记录

# Linux环境安装

# debain安装

# 环境准备

1. 更新软件包
sudo apt-get update

2. nginx需要gcc支持
sudo apt-get install build-essential

3. nginx需要正则表达式支持
sudo apt-get install libpcre3 libpcre3-dev

4. http需要gzip压缩
sudo apt-get install zlib1g-dev

5. SSL协议支持
sudo apt-get install openssl libssl-dev

# 下载安装

1. 下载nginx源码到当前目录
wget "http://nginx.org/download/nginx-1.20.1.tar.gz"
2. 将nginx解压到目标目录
tar -zxvf /root/nginx-1.20.1.tar.gz -C ./
3. 编译安装
cd nginx-1.20.1
默认配置
./configure
make && make install

# 修改nginx的conf文件


user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#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;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    # 开启gzip压缩
    gzip on;
    # 不压缩临界值,大于1K的才压缩,一般不用改
    gzip_min_length 1k;
    # 压缩缓冲区
    gzip_buffers 16 64K;
    # 压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_http_version 1.1;
    # 压缩级别,1-10,数字越大压缩的越好,时间也越长
    gzip_comp_level 5;
    # 进行压缩的文件类型
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
    # 跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding"
    gzip_vary on;
    # IE6对Gzip不怎么友好,不给它Gzip了
    gzip_disable "MSIE [1-6]\.";
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   E:/Coding/Learning2021/learning-projects/imooc-datav-report-dev/dist;
            # root   E:/Coding/Projects2023/IENC-datav/datav;
            root E:/03_Coding/Projects2024/Chs-Mission-Manage/ruoyi-ui/dist;
            index  index.html index.htm;
        }

		# 代理通向后端
        location /prod-api/ {
          proxy_set_header Host $http_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://localhost:8080/;
        }
        #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;
        }
    }
}

# 运行

1. 进入目录
cd /usr/local/nginx/sbin
2. 启动
./nginx 
3. 关闭
./nginx -s stop
4. 重启
./nginx -s reload

# 卸载

# 卸载Nginx
sudo apt-get remove nginx

# 设置开机自启动

首先创建开机自启动脚本

cd /etc/systemd/system
vi nginx.service

写入内容

[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid  
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置开机自启动

systemctl enable nginx

停止开机自启动

systemctl disable nginx.service

# Nginx常用命令

// 关闭所有nginx进程
taskkill /f /t /im nginx.exe

// 开启防火墙
sudo apt-get install iptables

# 反向代理案例

geoserver屏蔽浏览器端对管理员界面的访问,只提供地图服务。

# 方案一(推荐)

geoserver部署在8080端口,另起一个8081端口,地图服务代理到8081

    server {
        listen 8081;
        server_name  test.com;

        location /api {
            proxy_pass http://127.0.0.1:8080/api;
            proxy_set_header Host $host;
            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 $scheme;
        }
    }

# 方案二

把8080端口代理到8081,同时对管理员界面进行重定向,例如错误页面

    server {
        listen 8081;
        server_name  test.com;

        location / {
            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_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location /geoserver/web {
            rewrite ^/geoserver/web(/.*)?$ error.html permanent;
        }
    }

或者可以直接和前端的代码写在一起

Last Updated: 12/23/2024, 4:18:13 AM