Nginx系列之-基础配置详解
文章目录
- 前言
- 一、Nginx.conf
- 二、个人使用配置
- 1、nginx.conf配置
- 2、conf.d/文件夹配置文件
- 总结
前言
之前也零零碎碎使用过Nginx,以及实现过最基础的配置,以及成功运运行,但是没有真正系统的使用,自从成为架构师以后,公司没有运维,什么都要自己捣鼓,又担心自己做的不够细,不够严谨,出现问题,造成损失,所以不断花时间巩固之气那自己锁涉及的技术知识点,而不是仅仅会用了。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Nginx.conf
Nginx的安装我先不说了,非常的简单一条简单的sudo apt-get install nginx即可。下面我从:cd /etc/nginx路径copy的Nginx.conf我查查资料做个详细的说明输出。
user nginx;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2800;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
include /etc/nginx/conf.d/*.conf;
}
- user nginx; #用户
- worker_processes 4; #初始可设置为CPU总核数
- pid /run/nginx.pid; #用于管理nginx进程
- worker_connections 2800; #单个进程最大连接数(最大连接数=连接数*进程数)
- sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
- tcp_nopush on; #防止网络阻塞
- tcp_nodelay on; #防止网络阻塞
- keepalive_timeout 120; #长连接超时时间,单位是秒
- types_hash_max_size 影响散列表的冲突率。types_hash_max_size越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升。
- include mime.types; #文件扩展名与文件类型映射表
- default_type application/octet-stream; #默认文件类型
- access_log 主机的访问日志
- error_log 错误日志
- gzip on; #开启gzip压缩输出
- include /etc/nginx/conf.d/*.conf; #后续的配置我基本都会放在这个文件夹
二、个人使用配置
1、nginx.conf配置
user nginx;
worker_processes 4;
pid /var/run/nginx.pid;
#pid /run/nginx.pid
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
sendfile on;
client_max_body_size 2048m;
keepalive_timeout 65;
underscores_in_headers on;
#add_header Access-Control-Allow-Origin *;
#add_header Access-Control-Allow-Headers X-Requested-With;
#add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
include /etc/nginx/conf.d/*.conf; upstream whyt { server 192.168.0.199:8080; } }
2、conf.d/文件夹配置文件
配置文件:whyt-gateway.conf
server {
listen 60080;
#server_name localhost;
server_name 外网ip;
# zuul
location /whyt-gateway/ {
rewrite /whyt-gateway/(.*) /$1 break;
proxy_pass http://whyt;
#add_header Access-Control-Allow-Origin *;
#add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
#add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# whyt-backend
location / {
root /opt/nginx/whyt-backend/dist;
#try_files $uri $uri/ @router;
try_files $uri $uri/ /index.html last
index index.html;
}
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 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
总结
配置不做详细讲解,我这边就暴漏了两个路由一个业务Api一个后台管理系统Api。
还没有评论,来说两句吧...