一江明月 發表於 2022-6-9 14:17:00

nginx单域名多站点配置

<h2 id="nginx-单域名多站点配置">nginx 单域名多站点配置</h2>
<h3 id="背景">背景</h3>
<p>单台服务器上只依靠nginx实现多站点,有两种方式</p>
<ul>
<li>端口区分</li>
<li>通过路径区分</li>
</ul>
<h3 id="1端口区分">1、端口区分</h3>
<pre><code>http {

include mime.types;
gzip on;
server {
    listen 8080;
    location / {
      root /opt/nginx/html/site1/;
      index index.html;
    }
}
server {
    listen 8081;
    location / {
      root /opt/nginx/html/site1/;
      index index.html;
    }
   
}
keepalive_timeout 65;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;   
}
</code></pre>
<h3 id="2路径区分">2、路径区分</h3>
<p>路径区分又分为两种</p>
<ul>
<li>转发站点</li>
<li>转发负载</li>
</ul>
<h4 id="21转发站点">2.1、转发站点</h4>
<p>站点转发存在加载静态资源问题,因为转发指示将首页转发过去,首页加载出来后再去加载静态资源,就直接加载hosts/的静态资源,会导致加载不到,导致页面的样式,图片都无法显示,直接就是404报错</p>
<p><strong>解决办法</strong></p>
<p>通过<code>referer</code>解决,<code>referer</code>可以用来区分转发的来源</p>
<pre><code>http {

include mime.types;
gzip on;
server {

listen 80;
location / {
      proxy_passhttp://my-server;
      proxy_redirect default;
}
#location 位置带/与不带/ 无影响,
location /site1/ {
      proxy_pass   http://localhost:8080/;
      #转发要注意带/与不带的区别
      #转发要带/,才能去掉访问的路径,不然多一层目录找不到资源
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
      if ($http_referer ~ 'site1'){
          proxy_pass   http://localhost:8080;
      }
}
location /site2/ {
      proxy_pass   http://localhost:8081/;
}
}
server {
    listen 8080;
    location / {
      root /opt/nginx/html/site1/;
      index index.html;
    }
}
server {
    listen 8081;
    location / {
      root /opt/nginx/html/site2/;
      index index.html;
    }
   
}
keepalive_timeout 65;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;   
}
</code></pre>
<h4 id="22转发负载">2.2、转发负载</h4>
<p>这种方式比较简单,示例中给了两个站点负载,如果请求全部是相对路径,只需要一个即可,如果有绝对路径也是要通过referer解决</p>
<pre><code>http {

include mime.types;
gzip on;

upstreammy-server {
   server    localhost:8080;
   server    localhost:8081;
}

server {

listen 80;
location /site {
      proxy_passhttp://my-server;
      proxy_redirect default;
}
}
server {
    listen 8080;
    location / {
      root /opt/nginx/html/site1/;
      index index.html;
    }
}
server {
    listen 8081;
    location / {
      root /opt/nginx/html/site1/;
      index index.html;
    }
   
}
keepalive_timeout 65;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;   
}
</code></pre>
<h3 id="替换路径字符串">替换路径字符串</h3>
<p>场景:针对与替换路径中的字符串问题,可以快速解决路径多一层,少一层的问题</p>
<pre><code>rewrite ^/tianzehao/(.*)$ /$1 break;#匹配路径以/tianzehao开头的字符串路径,替换为(.*)$
# 例如/tianzehao/asd/asdjkj/index.html ,替换为/asd/asdjkj/index.html
</code></pre>
<h3 id="另外附rewrite说明">另外附rewrite说明</h3>
<p>TroubleShoot</p>
<h4 id="1域名解析重定向问题">1.域名解析重定向问题</h4>
<p>场景:域名elb “www.asdu.cn”解析到服务器8080端口,在访问子目录www.asdu.cn/asd时被重定向到www.asdu.cn:8080/asd导致无法访问<br>
原因: 访问网站的子目录时少"/"会被nginx重定向,nginx的server下指令port_in_redirect是默认开启的,转重定向时加上端口</p>
<p><code>解决:</code>port_in_redirect off即可</p>
<pre><code>   server {
      listen       8080;
      port_in_redirect off;
      XXXXX
   }
</code></pre>
<h4 id="2日志输出排查问题">2、日志输出排查问题</h4>
<p>场景:输出日志没有访问的目录,找不到访问的路径无法排查问题<br>
<code>解决:</code> $document_root 变量是可以打印相关日志</p>
<pre><code>#$document_root$document_uri 能组成一个完成的访问路径
log_formatmain'$remote_addr - $remote_user $document_root$document_uri[$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';
</code></pre>
<p><strong>附:这位大佬整理的</strong></p>
<pre><code>$args                  #请求中的参数值
$query_string            #同 $args
$arg_NAME                #GET请求中NAME的值
$is_args               #如果请求中有参数,值为"?",否则为空字符串
$uri                     #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"。
$document_uri            #同 $uri
$document_root         #当前请求的文档根目录或别名
$host                  #优先级:HTTP请求行的主机名&gt;"HOST"请求头字段&gt;符合请求的服务器名.请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称
$hostname                #主机名
$https                   #如果开启了SSL安全模式,值为"on",否则为空字符串。
$binary_remote_addr      #客户端地址的二进制形式,固定长度为4个字节
$body_bytes_sent         #传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
$bytes_sent            #传输给客户端的字节数
$connection            #TCP连接的序列号
$connection_requests   #TCP连接当前的请求数量
$content_length          #"Content-Length" 请求头字段
$content_type            #"Content-Type" 请求头字段
$cookie_name             #cookie名称
$limit_rate            #用于设置响应的速度限制
$msec                  #当前的Unix时间戳
$nginx_version         #nginx版本
$pid                     #工作进程的PID
$pipe                  #如果请求来自管道通信,值为"p",否则为"."
$proxy_protocol_addr   #获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串
$realpath_root         #当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径
$remote_addr             #客户端地址
$remote_port             #客户端端口
$remote_user             #用于HTTP基础认证服务的用户名
$request               #代表客户端的请求地址
$request_body            #客户端的请求主体:此变量可在location中使用,将请求主体通过proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass传递给下一级的代理服务器
$request_body_file       #将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传 递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off,uwsgi_pass_request_body off,or scgi_pass_request_body off
$request_completion      #如果请求成功,值为"OK",如果请求未完成或者请求不是一个范围请求的最后一部分,则为空
$request_filename      #当前连接请求的文件路径,由root或alias指令与URI请求生成
$request_length          #请求的长度 (包括请求的地址,http请求头和请求主体)
$request_method          #HTTP请求方法,通常为"GET"或"POST"
$request_time            #处理客户端请求使用的时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$request_uri             #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:"/cnphp/test.php?arg=freemouse"
$scheme                  #请求使用的Web协议,"http" 或 "https"
$server_addr             #服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中
$server_name             #服务器名
$server_port             #服务器端口
$server_protocol         #服务器的HTTP版本,通常为 "HTTP/1.0" 或 "HTTP/1.1"
$status                  #HTTP响应代码
$time_iso8601            #服务器时间的ISO 8610格式
$time_local            #服务器时间(LOG Format 格式)
$cookie_NAME             #客户端请求Header头中的cookie变量,前缀"$cookie_"加上cookie名称的变量,该变量的值即为cookie名称的值
$http_NAME               #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",使用$http_accept_language即可
$http_cookie
$http_host               #请求地址,即浏览器中你输入的地址(IP或域名)
$http_referer            #url跳转来源,用来记录从那个页面链接访问过来的
$http_user_agent         #用户终端浏览器等信息
$http_x_forwarded_for
$sent_http_NAME          #可以设置任意http响应头字段;变量名中的后半部分NAME可以替换成任意响应头字段,如需要设置响应头Content-length,$sent_http_content_length即可
$sent_http_cache_control
$sent_http_connection
$sent_http_content_type
$sent_http_keep_alive
$sent_http_last_modified
$sent_http_location
$sent_http_transfer_encoding
</code></pre>
<h4 id="3路径重复问题">3、路径重复问题</h4>
<p>3.1、静态资源目录重复</p>
<p>现象</p>
<pre><code>location /tianzehao {
    alias/var/www/html/tianzehao/;
    index index.html;
}
location /xiaoyu {
    root /var/www/html/xiaoyu;
    index index.html;
}

</code></pre>
<p><code>解决</code></p>
<p>nginx的<code>root</code>是:</p>
<pre><code>location部分附加到root部分
最终路径 = root + location
</code></pre>
<p>nginx的<code>alias</code>是:</p>
<pre><code>location部分被alias部分替换
最终路径 = alias
</code></pre>
<h4 id="32proxy的目录重复问题">3.2、proxy的目录重复问题</h4>
<p>配置如下:</p>
<pre><code>http {
    include       mime.types;
    default_typeapplication/octet-stream;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;
    upstream xiaoyuweb{
      server localhost:8082;
    }
    server {
      listen       80;

      location /xiaoyu123 {
             proxy_pass http://xiaoyuweb;
      }

    }
    server {
      listen       8082;

      location / {
            root   /var/www/html/xiaoyu;
            indexindex.html index.htm;

      }

    }
}
</code></pre>
<p>访问日志中多了 location的位置一层</p>
<p><code>解决:</code> 在转发结束位置加上“/”</p>
<pre><code>location /xiaoyu123 {
             proxy_pass http://xiaoyuweb/;
      }
</code></pre><br><br>
来源:https://www.cnblogs.com/zakun/p/nginx-vhost.html
頁: [1]
查看完整版本: nginx单域名多站点配置