服务器部署nginx

Nginx 可以作为静态文件的 Web 服务器,处理 HTML、CSS、JavaScript、图片等静态资源的请求,服务器通过docker 部署nginx 主要包括以下步骤:

1.编写docker-compose.yml文件

services:
      nginx:
        image: nginx
        restart: always
        network_mode: tpm
        container_name: gotion_base_nginx
        ports:
          - 19070:19070
        volumes:
          - /opt/gotion-base/nginx/nginx.conf:/etc/nginx/nginx.conf
          - /opt/gotion-base/nginx/cert/:/etc/cert/
          - /opt/gotion-base/nginx/base/dist/:/usr/share/nginx/html/
          - /data/gotion-base/nginx/logs/:/var/log/nginx/
          - /etc/localtime:/etc/localtime:ro

2.编写nginx.conf

user nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

        #nginx中的proxy_read_timeout指令用于设置代理连接的超时时间。当nginx作为代理服务器转发请求给后端服务器时,如果后端服务器在指定的时间内没有响应,nginx将终止连接并返回错误
    proxy_read_timeout 6000s;

    # 限制body大小
    client_max_body_size 100m;

    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/gotion-base/nginx/access.log  main;
     upstream server {
        ip_hash;
        server 10.2.128.45:9070;
       }

    server {
        listen       19070;
        server_name  10.2.128.45;
         location / {
            root   /usr/share/nginx/html/;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        location /dev-tpm/ {
            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://server/;
        }

        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://server/;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

3.在docker-compose.yml文件同级目录下执行docker-compose up -d

4.检查容器中nginx是否正常运行

5.问题解决

1.查看/data/gotion-base/nginx/logs/ 目录下的nginx访问和错误日志

2.通过docker exec -it my-nginx /bin/ bash 命令查看nginx.conf和前端dist包是否正常挂载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值