nginx之配置文件公用抽取

因为某些原因,需要同时部署同一应用两个不同分支的代码,而配置文件存在较大重复,因此有此篇。

最近构建的过程中遇到了一些跟nginx配置相关的问题,记录下。

简单说下构建的状况:前后端分离,jenkins + jar包+nginx部署前端。

#user  nobody;
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 {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$upstream_addr $remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; # 服务器有限,暂且部署到当前服务器上
upstream applicationName_server {
ip_hash;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
} server {
listen 8888;
server_name localhost;
# 上传文件大小限制100M
client_max_body_size 100m; #charset koi8-r; #access_log logs/host.access.log main;
add_header BackendIP "$upstream_addr;" always; set $ROOT_HTML /opt/soft/applicationName/backend/ui/html; location ^~/applicationName/login {
proxy_pass http://applicationName_server;
add_header 'Cache-Control' 'no-cache';
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; }
location ^~/applicationName/logout {
proxy_pass http://applicationName_server;
add_header 'Cache-Control' 'no-cache';
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;
} location /applicationName/api {
proxy_pass http://applicationName_server;
add_header 'Cache-Control' 'no-cache';
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;
}
location ^~/applicationName/cas {
proxy_pass http://applicationName_server;
add_header 'Cache-Control' 'no-cache';
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;
}
location /applicationName/artery {
proxy_pass http://applicationName_server;
add_header 'Cache-Control' 'no-cache';
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;
} location /applicationName/artery/code {
proxy_pass http://applicationName_server/applicationName/code/;
add_header 'Cache-Control' 'no-cache';
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;
} location ^~/static {
root $ROOT_HTML/applicationName;
} location / {
root $ROOT_HTML;
rewrite ^ /applicationName break;
}
location /applicationName {
if ($http_cookie = "") {
add_header Set-Cookie "thguid=$request_id;Path=/;Max-Age=315360000" always;
rewrite ^ /index.html break;
}
root $ROOT_HTML;
index index.html;
try_files $uri $uri/ /index.html; } location ~* ^.+\.(ico|js|gif|jpg|jpeg|png)$ {
root $ROOT_HTML;
try_files $uri $uri/ /index.html =404;
expires 1d;
} location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
root $ROOT_HTML;
try_files $uri $uri/ /index.html =404;
expires 7d;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} } server {
listen 7777;
server_name localhost;
# 上传文件大小限制100M
client_max_body_size 100m; #charset koi8-r; #access_log logs/host.access.log main;
add_header BackendIP "$upstream_addr;" always; set $ROOT_HTML /opt/soft/applicationName/backend/ui/otherhtml; location ^~/applicationName/login {
proxy_pass http://127.0.0.1:8087;
add_header 'Cache-Control' 'no-cache';
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; }
location ^~/applicationName/logout {
proxy_pass http://127.0.0.1:8087;
add_header 'Cache-Control' 'no-cache';
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;
} location /applicationName/api {
proxy_pass http://127.0.0.1:8087;
add_header 'Cache-Control' 'no-cache';
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;
}
location ^~/applicationName/cas {
proxy_pass http://127.0.0.1:8087;
add_header 'Cache-Control' 'no-cache';
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;
}
location /applicationName/artery {
proxy_pass http://127.0.0.1:8087;
add_header 'Cache-Control' 'no-cache';
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;
} location /applicationName/artery/code {
proxy_pass http://127.0.0.1:8087/applicationName/code/;
add_header 'Cache-Control' 'no-cache';
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;
} location ^~/static {
root $ROOT_HTML/applicationName;
} location / {
root $ROOT_HTML;
rewrite ^ /applicationName break;
}
location /applicationName {
if ($http_cookie = "") {
add_header Set-Cookie "thguid=$request_id;Path=/;Max-Age=315360000" always;
rewrite ^ /index.html break;
}
root $ROOT_HTML;
index index.html;
try_files $uri $uri/ /index.html; } location ~* ^.+\.(ico|js|gif|jpg|jpeg|png)$ {
root $ROOT_HTML;
try_files $uri $uri/ /index.html =404;
expires 1d;
} location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
root $ROOT_HTML;
try_files $uri $uri/ /index.html =404;
expires 7d;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} } }

刚开始的时候只监听了8888端口(后端端口8081,8082),用来构建dev分支代码。后来,需要构建出一个新的分支,又不能影响dev分支。因此新增一个7777端口进行监听(后端对应端口8087),两个server除了端口不同外就只有proxy_pass对应的地址不同,html的地址不同,因此,考虑将相同部分抽出来。

在conf同级新建conf.d文件夹,新建dev.conf和performance.conf

server {
listen 8888;
server_name localhost;
# 上传文件大小限制100M
client_max_body_size 100m; #charset koi8-r; #access_log logs/host.access.log main;
add_header BackendIP "$upstream_addr;" always; set $ROOT_HTML /opt/soft/applicationName/backend/ui/html;
set $SERVER_URL applicationName_server;
include common-route.conf; }
server {
listen 7777;
server_name localhost;
# 上传文件大小限制100M
client_max_body_size 100m; #charset koi8-r; #access_log logs/host.access.log main;
add_header BackendIP "$upstream_addr;" always; set $ROOT_HTML /opt/soft/applicationName/backend/ui/otherhtml;
set $SERVER_URL 127.0.0.1:8087;
include common-route.conf; }

这里将原本两个不同点通过设置ROOT_HTMLSERVER_URL来解决。其余的内容放到conf文件夹下的common-router.conf

# 通用路由
location ^~/applicationName/login {
proxy_pass http://$SERVER_URL;
add_header 'Cache-Control' 'no-cache';
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; }
location ^~/applicationName/logout {
proxy_pass http://$SERVER_URL;
add_header 'Cache-Control' 'no-cache';
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;
} location /applicationName/api {
proxy_pass http://$SERVER_URL;
add_header 'Cache-Control' 'no-cache';
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;
}
location ^~/applicationName/cas {
proxy_pass http://$SERVER_URL;
add_header 'Cache-Control' 'no-cache';
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;
}
location /applicationName/artery {
proxy_pass http://$SERVER_URL;
add_header 'Cache-Control' 'no-cache';
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;
} location /applicationName/artery/code {
#return 200 http://$SERVER_URL/applicationName/code;
rewrite ^/(.*)/code/(.*)$ /applicationName/code/$2 permanent;
#proxy_pass $CODE_URL;
#proxy_pass http://$SERVER_URL/applicationName/code/;
#proxy_pass http://127.0.0.1:8087/applicationName/code/;
add_header 'Cache-Control' 'no-cache';
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;
}
location /applicationName/code {
proxy_pass http://$SERVER_URL;
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;
} location ^~/static {
root $ROOT_HTML/applicationName;
} location / {
root $ROOT_HTML;
rewrite ^ /applicationName break;
}
location /applicationName {
if ($http_cookie = "") {
add_header Set-Cookie "thguid=$request_id;Path=/;Max-Age=315360000" always;
rewrite ^ /index.html break;
}
root $ROOT_HTML;
index index.html;
try_files $uri $uri/ /index.html; } location ~* ^.+\.(ico|js|gif|jpg|jpeg|png)$ {
root $ROOT_HTML;
try_files $uri $uri/ /index.html =404;
expires 1d;
} location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
root $ROOT_HTML;
try_files $uri $uri/ /index.html =404;
expires 7d;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

nginx.conf如下

#user  nobody;
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 {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$upstream_addr $remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; # 服务器有限,暂且部署到当前服务器上
upstream applicationName_server {
ip_hash;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
} include /usr/local/nginx/conf.d/*.conf; }

虽然上面的配置平平无奇,不过走向正确配置的道路还是遇到了些波折。

  • conf.d文件夹下的两个conf文件的include文件的路径问题,..啥的用不了,反倒是看起来不正确的 include common-route.conf;能够正确引用。另一点是,common-route.conf不能放到conf.d下面,报的错忘了什么了。

  • 原先conf.dconf文件的SERVER_URL是这样的http://applicationName_server,后来报了invalid port,把http://去掉放到具体的server里面了。

  • 单值代码加载,在未做改造前这样

    location /applicationName/artery/code {
    proxy_pass http://applicationName_server/applicationName/code/;
    add_header 'Cache-Control' 'no-cache';
    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;
    }

    第一次改造

    location /applicationName/artery/code {
    
    	proxy_pass http://$SERVER_URL/applicationName/code/;
    
        add_header 'Cache-Control' 'no-cache';
    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;
    }

    这种直接404,而如果把$SERVER_URL改成具体的后端地址,则7777和8888端口中有一个可以正常访问单值代码。谷歌的几个方案都说是什么resolver的问题,但照着改还是不工作

    再后来改造使用rewrite来改写path

       location /applicationName/artery/code {
    
                    rewrite  ^/(.*)/code/(.*)$ /applicationName/code/$2  permanent;
    
                       add_header 'Cache-Control' 'no-cache';
    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;
    }

    路径倒是改了(忽略上面的$2,去掉第一个括号可以改成$1),可applicationName/code又404了。然后又加上了/applicationName/code的location配置

    location /applicationName/artery/code {
    #return 200 http://$SERVER_URL/applicationName/code;
    rewrite ^/(.*)/code/(.*)$ /applicationName/code/$2 permanent;
    #proxy_pass $CODE_URL;
    #proxy_pass http://$SERVER_URL/applicationName/code/;
    #proxy_pass http://127.0.0.1:8087/applicationName/code/;
    add_header 'Cache-Control' 'no-cache';
    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;
    }
    location /applicationName/code {
    proxy_pass http://$SERVER_URL;
    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;
    }

最后,附个conf的目录结构

nginx之配置文件公用抽取的更多相关文章

  1. Nginx的配置文件(nginx.conf)解析和领读官网

    步骤一:vi nginx.conf配置文件,参考本博文的最下面总结,自行去设置 最后nginx.conf内容为 步骤二:每次修改了nginx.conf配置文件后,都要reload下. index.ht ...

  2. [原]生产环境下的nginx.conf配置文件(多虚拟主机)

    [原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...

  3. Nginx源码研究六:NGINX的配置文件分析

    上一篇写到nginx的各个模块的配置信息的存储结构,大体描述了对配置信息的配置项生成,定制,初始化过程.这里重点研究实现定制的过程,所谓实现定制,这里指的是,nginx系统提供使用者定义nginx的配 ...

  4. Nginx 主配置文件参数详解

    Nginx 主配置文件参数详解 Nginx 安装完毕后,会有响应的安装目录,安装目录里 nginx.conf 为 nginx 的主配置文件, ginx 主配置文件分为 4 部分,main(全局配置). ...

  5. Nginx的配置文件nginx.conf解析

    安装openresty的nginx.conf配置文件 0.ng运行的用户和用户组 1.ng进程数,设置为CPU总核心数 2.ng错误日志 3.进程文件,有时ng启动不了,将进程文件删除即可. 4.单进 ...

  6. Nginx入门讲解——初步认识了解nginx.conf配置文件以及配置多个虚拟主机

    本文引自网络进攻学习之用https://blog.csdn.net/weixin_38111957/article/details/81080539 一. 引言上节文章讲述了如何用信号控制Nginx服 ...

  7. Nginx(二)------nginx.conf 配置文件

    上一篇博客我们将 nginx 安装在 /usr/local/nginx 目录下,其默认的配置文件都放在这个目录的 conf 目录下,而主配置文件 nginx.conf 也在其中,后续对 nginx 的 ...

  8. 通过python生成nginx模板配置文件

    通过python生成nginx模板配置文件 # cat config.py #coding=utf-8 nginx_conf = ''' server {{ listen {port}; server ...

  9. 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置

    第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本  uwsgi- ...

随机推荐

  1. 微服务入门二:SpringCloud(版本Hoxton SR6)

    一.什么是SpringCloud 1.官方定义 1)官方定义:springcloud为开发人员提供了在分布式系统中快速构建一些通用模式的工具(例如配置管理.服务发现.断路器.智能路由.微代理.控制总线 ...

  2. [2022-3-5] OICLASS-USACO提高组模拟赛2 B: Cow Frisbee

    题意 在一排奶牛中,对于每两头奶牛,如果两头奶牛之间没有奶牛比这两头高,则答案累加这两头奶牛的距离. 分析 设现在分析的奶牛为第 \(i\) 头,它向左扔出了一个飞盘,显然它的飞行高度为奶牛的高度.飞 ...

  3. 【VR游戏】封神榜传

    领悟导入SteamVR和VRTK,SteamVR要和CameraRig位置保持一致.1.可以创建多个scene,然后进行连接跳转.但是创建好之后要自己重新导回scene附:删除MainCamera,添 ...

  4. Netty学习(二)使用及执行流程

    Netty简单使用 1.本文先介绍一下 server 的 demo 2.(重点是这个)根据代码跟踪一下 Netty 的一些执行流程 和 事件传递的 pipeline. 首先到官网看一下Netty Se ...

  5. CF1386C口胡

    自己在物理课上编了一道题,大概就是这题把删除区间的边改为保留区间的边...都不觉得判断短路和判断二分图有点儿像吗 题意:给定一张无向图,每次暂时删除一个区间内的边,问删除后这个区间是否为二分图. 首先 ...

  6. Linux环境下安装RocketMQ

    最近在学习消息队列,针对RocketMQ进行了初步研究,这里记录下安装配置的过程,与大家共同分享 一.选择合适的版本 注:安装.运行过程中需要依赖JDK,因此安装之前需要保证当前linux环境下具备上 ...

  7. AQS 详解之共享锁模式

    概括 AQS框架数据结构是一个先进先出的双向队列,当多个线程进行竞争资源时,那些竞争失败的线程会加入到队列中.他向上层提供了很多接口,其中一个是acquireShared获取共享模式的接口.本文将会根 ...

  8. eclipse中的项目如何打成war包

    war包即Web归档文件,将Web项目打成war包可以直接拷贝到Web服务器发布目录(例如Tomcat服务器webapps目录 ),当Tomcat启动后该压缩文件自动解压,war包方便了web工程的发 ...

  9. django之model,crm操作

    一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pr ...

  10. web自动化之定位

    UI自动化必不可少的操作--元素定位 8大基础定位 driver.find_element_by_id() # id定位 driver.find_element_by_name() # name定位 ...