Wonderjar's Blog

Nginx基本配置备份

2018-04-18

因为部署前端项目经常需要配nginx,这里备份一份配置,方便查询

基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
events {
worker_connections 1024;
}

http {

include mime.types;
default_type application/octet-stream;

server {
listen 8080 default_server;

root /XXX;

location / {
index index.html;
try_files $uri $uri/ /index.html;
}

location ^~ /api/ {
proxy_pass http://127.0.0.1:8081/;
}

}
}

Run in Docker

如果很多项目需要nginx,觉得相互影响,也可以写Dockerfile,让各个项目启一个nginx的docker container,这里也备份一份,

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
FROM nginx
MAINTAINER Jar jarancn@gmail.com
COPY docker/nginx.conf /etc/nginx/conf.d/

Dockerfile.sh

#!/usr/bin/env bash
docker rmi -f DOCKER_IMAGE_NAME | true
docker build --rm -t DOCKER_IMAGE_NAME -f docker/Dockerfile .
docker rm -f DOCKER_CONTAINER_NAME | true
docker run -p 8030:80 --name DOCKER_CONTAINER_NAME -v /Users/XXX/project/dist:/usr/share/nginx/html -d DOCKER_IMAGE_NAME

这里做了一个挂载,如果只是稳定的跑起来,也可以在Dockerfile中把dist拷进去

COPY dist /usr/share/nginx/html

nginx相关命令也记一个
./sbin/nginx -c /opt/nginx/nginx.conf
./sbin/nginx -s reload|reopen|stop|quit -c /opt/nginx/nginx.conf

Enable Gzip

1
2
3
4
5
6
7
8
9
##
# Gzip Settings
##
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

避免单页应用index.html被缓存

1
2
3
location = /index.html {
add_header Cache-Control no-cache;
}
Tags: Nginx