nginx[新手用][模块化][高效]配置

零.安装与下载

略过

一. 项目结构

主站的话,我是放一个静态网页,方便过域名审核之类的。自己用的其他业务使用二级域名或者在顶级域名下通过路由``反向代理
创建目录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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康娜喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值