ningx.conf
本人刚开始使用nginx的时候,不熟悉配置文件如何实现自己想要的访问路径与实际项目映射关系,还是爬过些坑的,这里和大家分享一下本人知道的一些nginx配置文件细节。
下面是一份配置文件 nginx.conf,这里假设服务起域名是www.test.com(配置文件是真实存在的有效,仅原域名www.xxx.com被改为www.test.com)。它实现了以下路径访问效果:
1.http://www.test.com --> 访问网站首页:html/index.html 这里html是 nginx启动程序所在目录下的一个文件夹,安装过nginx应该知道
2.http://www.test.com/note/online --> 访问springboot项目controller层接口 note 是一个springboot项目 ,/onlie 接口:获取在线人数
3.http://www.test.com/stc/jquery.js --> 访问静态文件 服务器上一下静态文件
4.http://www.test.com/git --> 访问GitLab首页 已经安装了GitLab私服,使用的是自己的nginx,没有用gitlab中集成的nginx
user root root; //这里用户是root 可能权限大了点,需要指出:这里配置root还有一个原因是本人安装gitlab时配置文件指定用户为root,避免访问gitlab时可能有权限冲突,将gitlab用户与nginx的配置文件中用户设置为一致。 worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #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; } http { include 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"'; 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配置文件中搬过来的 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; } # 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/;
#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.emoyc.com; if ($invalid_referer) { return 404; } } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }