# 全局配置
#配置用户或者组,默认为nobody
#user nobody;
#允许生成的进程数,默认为1
worker_processes 1;
#制定错误日志路径,级别。这个设置可以放在全局块(当前),http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log;
#error_log logs/error.log notice; # notice 级别
#指定nginx进程运行文件存放地址
# pid logs/nginx.pid; # 默认地址
events {
# accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
# multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
# use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
# http 配置
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块
error_page 404 http://www.baidu.com; # http级别错误设置:404错误页跳转【百度】
# 将 http 配置拆分成一个个子配置文件(内含 server 配置),位置在 conf/extra/ 下的所有 .conf 文件
include extra/*.conf; # 这里的80端口在子配置文件中被监听了
#
# 除了用上面的 include 属性来讲子配置文件引入外,还可以就在该配置文件内书写多个 server 块
#
server {
listen 8000;
server_name muy1.cn;
root html; # 默认 html
error_page 404 /50x.html; # server 级别错误设置:当访问 127.0.0.1:80 状态码为 404 时,跳转至静态资源 50x.html
####################################################################### root 和 alias 知识点【开始】 ##########################################################
# 🙇请求规则 【127.0.0.1:80/】 该规则不设置也默认存在,设置后会覆盖默认
location / {
# 🧀 知识点
# 🧀 root 实际查找路径:root路径 + location 路径
# 🧀 alias 实际查找路径:alias 路径(替换了location路径)
root html/imgs; # root在指定目录下查找:在 html/imgs/ 目录下查找,默认是 html 目录
# alias html/imgs/; # alias在指定目录下查找:注意alias末尾必须加 / 否则报错:500 Internal Server Error
index 未知头像.jpg; # 设置默认打开:未知头像.jpg;如果不设置默认,就是查找目录下的 index.html
}
# 🙇请求规则 【127.0.0.1:80/imgs】
location /imgs {
# root abc/ ; # 查找路径:abc/imgs,默认是 html
# 还可以写成
alias abc/imgs; # 查找路径:abc/imgs,因为 location 路径结尾没有 /,所以 alias 结尾可以不加 /
index 未知头像.jpg;
}
# 🙇请求规则 【127.0.0.1:80/abc】
location /abc/ {
alias html/imgs/; # 因为 location 路径结尾有 /,所以 alias 结尾必须加 /
index 未知头像.jpg;
# error_page 404 http://www.qq.com; # 🔥请求级别错误设置:404错误页跳转【腾讯】
}
####################################################################### root 和 alias 知识点【结束】 ##########################################################
####################################################################### proxy_pass 知识点【开始】 ##########################################################
# 🧀 关于 proxy_pass 结尾加不加 /
# 1、结尾有 / ,最终的请求路径 = proxy_pass 路径(替换匹配的部分)
location /ser { # 127.0.0.1:80/ser
proxy_pass http://127.0.0.1:4396/; # 转发:http://127.0.0.1:4396/
}
# 2、结尾无 / ,最终请求路径 = proxy_pass 路径 + location 路径
# location /ser { # 此条就是作为演示
# proxy_pass http://127.0.0.1:4396; # 转发:http://127.0.0.1:4396/ser ,会附带 location 路径
# }
# 因为浏览器不识别代理,所以没有配置的其他请求还是走nginx服务器的80端口,因此还需要一一转发
# 🧀 由上可知,由于vue项目中还有大量的 /js、/css、/img 等等请求需要一一配置uri转发代理,而且 proxy_pass 结尾不能加 / (因为需要带上 location 路径查找文件)
location /css {
proxy_pass http://127.0.0.1:4396; # 转发:http://127.0.0.1:4396/css
}
location /js {
proxy_pass http://s1;
}
location /img{
proxy_pass http://s1;
}
location /fonts{
proxy_pass http://s1;
}
####################################################################### proxy_pass 知识点【结束】 ##########################################################
####################################################################### rewrit 知识点【开始】 ##########################################################
# 用到再说
####################################################################### rewrit 知识点【结束】 ##########################################################
}
# HTTPS server 暂时先不管
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
nginx基础配置
最新推荐文章于 2024-07-26 09:30:14 发布