nginx简单使用配置
使用nginx首先要明确使用场景,这里是一台服务器实现多种类型访问:网站首页访问,GitLab访问,note(私人springboot项目),静态文件访问。
下面是一份配置文件 nginx.conf,这里假设服务起域名是www.test.com(配置文件是从真实主机copy过来的,仅原域名www.xxx.com被改为www.test.com)。它实现了以下功能:
1.http://www.test.com-------------------------------------------> 访问网站首页:html/index.html 实际访问/usr/local/nginx/html/index.html
2.http://www.test.com/note/online---------------------------> 访问springboot项目controller层接口 note 是一个springboot项目,实际访问 127.0.0.1:11181/online
3.http://www.test.com/stc/jquery.js--------------------------> 访问静态文件 实际访问服务器静态文件夹位置 /usr/local/nginx/html/static/jquery.js
4.http://www.test.com/git--------------------------------------> 访问GitLab首页 已经安装了GitLab私服,使用的是自己的nginx,没有用gitlab中集成的nginx
nginx.conf:
user root root; #这里用户是root 可能权限大了点,需要指出:这里配置root还有一个原因是本人安装gitlab时配置文件指定用户为root,避免访问gitlab时可能有权限冲突,将gitlab用户与nginx的配置文件中用户设置为一致。
worker_processes 1; events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
log_format main '$time_local |$remote_addr-$remote_user| $http_host | $http_referer | $request | $status |$request_body | $body_bytes_sent|$http_user_agent|$http_x_forwarded_for';
access_log logs/test_access.log main; sendfile on; #gitlab节点 gitlab项目 真实访问入口
upstream gitlab-workhorse {
server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}
#note项目 真实访问入口
upstream note{
server 127.0.0.1:11181;
}
keepalive_timeout 65;
#gzip on;
#域名配置
server {
listen 80; #监听端口号 80
server_name www.test.com; #服务器域名
root html; #根目录
error_page 404 /404.html; #错误跳转
index index.html; #首页
#开启代理服务的404页面
#针对代理服务的404
proxy_intercept_errors on;
#charset koi8-r; #gitlab gitlab项目后缀名匹配,直接从gitlab的nginx配置文件中搬过来的
location ~ (\.git/gitlab-lfs/objects|\.git/info/lfs/objects/batch$) {
proxy_pass http://gitlab-workhorse;
proxy_request_buffering off;
} # /gitlab 项目配置路径
location /git {
proxy_pass http://gitlab-workhorse;
} # 静态路径匹配
location ^~ /stc {
alias html/static;
#//访问http://www.test.com/stc/jquery.js 实际访问的是 html/static/jquery.js
} # note项目
location /note {
#一些ip地址头信息预处理
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://note/;
#proxy_pass的note后面添加'/'有截断效果,有'/':http://www.test.com/note/online-->http://127.0.0.1:11118/online | 没有'/':http://www.test.com/note/online-->http://127.0.0.1:11181/note/online
#http://www.test.com/note/online 实际上访问的是 127.0.0.1:11181/online
error_page 404 /404.html;
} #防止盗链
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers www.test.com;
if ($invalid_referer) {
return 404;
}
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
} }
这里的nginx安装位置为:/usr/local/nginx/,启动程序nginx也在此处,所以配置文件中 root html; 前面没有'/' 表示相对路径,html 实际位置是 /usr/local/nginx/html
配置细节
location路径匹配规则 location [=|~|~*|^~|@] /uri/ { … }
=:精确匹配,url完全等于 等于号后面的值才会匹配
^~:url以常规字符匹配,如果匹配该选项,不匹配别的选项,本文中有用到来匹配静态文件目录
~:表示正则匹配,区分大小写
*~:表示正则匹配,但是不区分大小写
/:通用匹配,任何请求都可以匹配到
@:定义一个命名的location,用于内部定向,本文中未使用
alias 与 root 区别
alias 访问路径 截取
root 访问路径 拼接
# 静态路径匹配 这里列出访问一个静态文件 alisa 与 root 使用效果区别
location ^~ /stc {
alias html/static;
#http://www.test.com/stc/jquery.js --> html/static/jquery.js
}
location ^~ /stc {
root html/static;
#http://www.test.com/stc/jquery.js --> html/static/stc/jquery.js
}
本文不定期更新
nginx简单使用配置的更多相关文章
- nginx简单代理配置
原文:https://my.oschina.net/wangnian/blog/791294 前言 Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器, ...
- Nginx简单的配置详情
大致了解Nginx后,直接从配置文件入手: [shell] #定义Nginx运行的用户和用户组 user nginx; #nginx进程数,建议设置为等于CPU总核心数. worker_process ...
- nginx简单权限配置
一.指定ip段 location / { allow 172.17.0.1/24; deny all; } 二.指定认证账户 location / { auth_basic "please ...
- Docker 安装 Nginx 负载均衡配置
Docker 安装 # 1)安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 2)添加Docker软件包源(否则d ...
- Nginx 简单的负载均衡配置示例(转载)
原文地址:Nginx 简单的负载均衡配置示例(转载) 作者:水中游于 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www ...
- Nginx负载均衡配置简单配置方法
http://www.jb51.net/article/121235.htm Nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的Web服务器.下面通过 ...
- nginx 简单理解和配置
1.概念 Nginx是一个高性能的HTTP和反向代理的web服务器,同时也提供了IMAP/POP3/SMTP服务,Nginx是由伊戈尔·塞索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的,第一 ...
- Nginx简单配置
Nginx 配置文件结构如果你下载好啦,你的安装文件,不妨打开 conf 文件夹的 nginx.conf 文件,Nginx 服务器的基础配置,默认的配置也存放在此.在 nginx.conf 的注释符号 ...
- windows下nginx安装、配置与使用(转载)
目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...
随机推荐
- 【repost】前端学习总结(二十三)——前端框架天下三分:Angular React 和 Vue的比较
目录(?)[+] 前端这几年的技术发展很快,细分下来,主要可以分成四个方面: 1.开发语言技术,主要是ES6&7,coffeescript,typescript等: 2.开发框架,如Ang ...
- 用Django ORM实现树状结构
前言 之前看对于用关系数据库实现树状结构的方法就知道一直做自关联的表,但是感觉自关联查询太慢了,最近看到一篇文章,感觉视野开拓了好多,文章:数据库表设计,没有最好只有最适合来自:微信. 下面就针对这里 ...
- SSM_CRUD新手练习(9)显示分页数据
我们已经做好了用来显示数据的分页模板,现在只需要将我们从后台取出的数据填充好,显示出来. 我们使用<c:forEach>标签循环取出数据,所以需要先导入JSTL标签库 <%@ tag ...
- LOJ#6387 「THUPC2018」绿绿与串串 / String (Manacher || hash+二分)
题目描述 绿绿和 Yazid 是好朋友.他们在一起做串串游戏. 我们定义翻转的操作:把一个串以最后一个字符作对称轴进行翻转复制.形式化地描述就是,如果他翻转的串为 RRR,那么他会将前 ∣R∣−1个字 ...
- Nginx 负载均衡与反向代理
通过设置权重来轮询 weight server 192.168.1.62 weight=5 server 192.168.63 weight=1 ip_hash 第3方均衡策略 fair url_h ...
- shell 脚本中的当前工作目录等于执行脚本时所在的工作目录
1. 测试脚本 在当前目录下创建 h1 h2两个文件夹 2. 在脚本所在目录执行脚本 在当前目录下创建了h1 h2两个目录 3. 在其他目录爱=下执行脚本 在其他目录下执行脚本时,在执行脚本所在的目录 ...
- 200行代码实现简版react🔥
200行代码实现简版react
- 课程三(Structuring Machine Learning Projects),第一周(ML strategy(1)) —— 0.Learning Goals
Learning Goals Understand why Machine Learning strategy is important Apply satisficing and optimizin ...
- SignalR 中使用 MessagePack 序列化提高 WebSocket 通信性能
It's like JSON.but fast and small. MessagePack is an efficient binary serialization format. It lets ...
- Python编码和Unicode
原文链接: ERIC MORITZ 翻译: 伯乐在线- 贱圣OMG译文链接: http://blog.jobbole.com/50345/ 我确定有很多关于Unicode和Python的说明,但为 ...