零.安装与下载
略过
一. 项目结构
主站的话,我是放一个静态网页,方便过域名审核之类的。自己用的其他业务使用二级域名
或者在顶级域名
下通过路由``反向代理
。
创建目录www
,并在该目录下根据每个域名创建对应的文件夹存放。
例如,顶级域名对应的网站目录为
/www
└── at
├── cert
│ ├── at.key
│ └── at.pem
├── index.html
└── static
├── bgi.gif
├── favicon.ico
└── nya.gif
二. 配置
2.1 主配置
首先是默认的配置,几乎不需要修改,小网站也不需要改多高的并发。
创建了一个叫mini
的日志记录格式,主要就看一个IP
和请求方式
即可
# user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
然后在http
里创建我们日志的格式,并且导入我们的自己的配置。
即在/etc/nginx/
下创建MyCfg
文件夹,将每一个域名写入成一个配置,方便模块化管理与备份。
http {
include mime.types;
default_type application/octet-stream;
log_format mini '[$remote_addr] [$time_local] [$status] [$request_time] $request';
include /etc/nginx/MyCfg/*.conf;
}
2.2 单个网页配置
我这个网页的配置里,一共有两个server
:一个跳转http,一个https访问。
2.2.1 强制SSL
创建一个server
并且强制将其定向为https
server {
listen 80;
listen [::]:80;
server_name domain.com;
return 301 https://$host$request_uri;
}
2.2.2 SSL配置
这是server
里关于ssl
的配置
listen 443 ssl;
listen [::]:443 ssl;
server_name domain.com;
ssl_certificate /www/at/cert/at.pem;
ssl_certificate_key /www/at/cert/at.key;
2.2.3 常规项与优化项配置
其中主要为:字符集的选择
,log的位置
,一些优化项
,gzip设置
。具体含义不懂的话可以自行百度
charset utf-8;
access_log /var/log/nginx/index_access.log mini;
sendfile on;
tcp_nodelay on;
keepalive_timeout 15;
gzip on;
gzip_buffers 4 8K;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/x-javascript text/css text/javascript image/gif image/png;
gzip_vary on;
2.2.4 4X和5X的返回
放了一些错误页面的返回内容
error_page 400 403 404 /miss.html;
error_page 500 502 503 504 /err.html;
location = /miss.html {
internal;
default_type text/html;
add_header Content-Type 'text/html;charset=utf-8';
return 404 '看不到的就没有返回喵(`⌒´メ)';
}
location = /err.html {
internal;
default_type text/html;
add_header Content-Type 'text/html;charset=utf-8';
return 502 '服务器坏掉了喵(〃>_<;〃)';
}
2.2.5 主站
主站就一个静态html
,然后所有的资源请求从静态目录获取,并设置过期时间。网页的logo可以过期得更长一些。
location / {
root /www/at/;
index index.html;
}
location /static/ {
alias /www/at/static/;
expires 30d;
}
location = /favicon.ico {
root /www/at/static/;
expires 180d;
}
2.2.6 其他应用
反向代理即可
location /app/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
三. 预览
/etc/nginx/nginx.conf
:
# user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format mini '[$remote_addr] [$time_local] [$status] [$request_time] $request';
include /etc/nginx/MyCfg/*.conf;
}
/etc/nginx/MyCfg/at.conf
:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name domain.com;
charset utf-8;
access_log /var/log/nginx/index_access.log mini;
sendfile on;
tcp_nodelay on;
keepalive_timeout 15;
gzip on;
gzip_buffers 4 8K;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/x-javascript text/css text/javascript image/gif image/png;
gzip_vary on;
ssl_certificate /www/at/cert/at.pem;
ssl_certificate_key /www/at/cert/at.key;
error_page 400 403 404 /miss.html;
error_page 500 502 503 504 /err.html;
location = /miss.html {
internal;
default_type text/html;
add_header Content-Type 'text/html;charset=utf-8';
return 404 '看不到的就没有返回喵(`⌒´メ)';
}
location = /err.html {
internal;
default_type text/html;
add_header Content-Type 'text/html;charset=utf-8';
return 502 '服务器坏掉了喵(〃>_<;〃)';
}
location / {
root /www/at/;
index index.html;
}
location /static/ {
alias /www/at/static/;
expires 30d;
}
location = /favicon.ico {
root /www/at/static/;
expires 180d;
}
location /vscode/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
}
server {
listen 80;
listen [::]:80;
server_name domain.com;
return 301 https://$host$request_uri;
}