不太想努力 發表於 2021-11-22 18:15:00

nginx域名隐性(地址栏域名不变)跳转

<p>###</p>
<h2>1、前提</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">系统介绍
    系统A
      域名: http://yhjmp.test.com
      部署服务器:192.168.56.20<br>      服务器名称:A-system
    系统B
      域名: http://yh.test.com或者http://yh.test.com/B-system【本文做了301永久跳转】
      部署服务器:192.168.56.140<br>      服务器名称:B-system
    系统B附属系统
      域名: http://yh.test.com/B-system-bak
      部署服务器:192.168.56.140<br>      服务器名称:B-system
需求
  两套系统A、B使用统一域名 http://yhjmp.test.com</span><br><span style="color: rgba(0, 0, 0, 1)">  点击跳转按钮,可访问两套系统中内容,域名不变<br>  示例:浏览器地址栏中输入yhjmp.test.com域名不变访问的内容为yh.test.com上的内容<br></span></pre>
<img src="https://img2020.cnblogs.com/blog/1274745/202111/1274745-20211122170059181-420536418.png" alt="" loading="lazy"></div>
<h2>&nbsp;2、B系统-前端代码</h2>
<div class="cnblogs_code">
<pre># pwd
</span>/usr/share/nginx/<span style="color: rgba(0, 0, 0, 1)">html

# tree -L <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
.
├── B</span>-<span style="color: rgba(0, 0, 0, 1)">system
│   └── index.html
├── B</span>-system-<span style="color: rgba(0, 0, 0, 1)">bak
   └── index.html

# cat B-system/<span style="color: rgba(0, 0, 0, 1)">index.html
</span>&lt;meta http-equiv=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Content-Type</span><span style="color: rgba(128, 0, 0, 1)">"</span> content=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text/html; charset=utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
&lt;h1&gt; B ~ 系统&lt;/h1&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/A-system/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转A系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/B-system/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转B系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/B-system-bak/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转B系统附属系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;<span style="color: rgba(0, 0, 0, 1)">


# cat B-system-bak/<span style="color: rgba(0, 0, 0, 1)">index.html
</span>&lt;meta http-equiv=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Content-Type</span><span style="color: rgba(128, 0, 0, 1)">"</span> content=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text/html; charset=utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
&lt;h1&gt; B ~ 系统 ~ 附属系统&lt;/h1&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/A-system/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转A系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/B-system/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转B系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/B-system-bak/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转B系统附属系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;<br><br></pre>
</div>
<h2>3、A系统-前端代码</h2>
<div class="cnblogs_code">
<pre># pwd
</span>/usr/share/nginx/<span style="color: rgba(0, 0, 0, 1)">html

# tree -L <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
.
├── A</span>-<span style="color: rgba(0, 0, 0, 1)">system
   └── index.html

# cat A-system/<span style="color: rgba(0, 0, 0, 1)">index.html
</span>&lt;meta http-equiv=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Content-Type</span><span style="color: rgba(128, 0, 0, 1)">"</span> content=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text/html; charset=utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
&lt;h1&gt; A ~ 系统&lt;/h1&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/A-system/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转A系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/B-system/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转B系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;button onclick=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.location.href='/B-system-bak/index.html'</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;跳转B系统附属系统&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;</pre>
</div>
<h2>4、B系统nginx配置</h2>
<div class="cnblogs_code">
<pre># cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}
http {
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log/var/log/nginx/access.logmain;
    rewrite_log on;
    error_log /var/log/nginx/error.log notice;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type      application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    <span style="background-color: rgba(0, 255, 255, 1)">server {
</span>      <span style="background-color: rgba(0, 255, 255, 1)">server_nameyh.test.com;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">location /B-system {
</span>          <span style="background-color: rgba(0, 255, 255, 1)">root /usr/share/nginx/html;
</span>          <span style="background-color: rgba(0, 255, 255, 1)">try_files $uri $uri//B-system/index.html;
</span>          <span style="background-color: rgba(0, 255, 255, 1)">indexindex.html index.htm;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">}
</span>      <span style="background-color: rgba(0, 255, 255, 1)">location /B-system-bak{
</span>          <span style="background-color: rgba(0, 255, 255, 1)">root /usr/share/nginx/html;
</span>          <span style="background-color: rgba(0, 255, 255, 1)">try_files $uri $uri//B-system-bak/index.html;
</span>          <span style="background-color: rgba(0, 255, 255, 1)">indexindex.html index.htm;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">}
</span>      <span style="background-color: rgba(0, 255, 255, 1)">location / {
</span>          <span style="background-color: rgba(0, 255, 255, 1)">rewrite ^/(.*) <span style="text-decoration: underline">http://yh.test.com/B-system</span> permanent;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">}
</span>    <span style="background-color: rgba(0, 255, 255, 1)">}</span>
}</pre>
</div>
<h2>&nbsp;5、A系统nginx配置</h2>
<div class="cnblogs_code">
<pre># cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log/var/log/nginx/access.logmain;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type      application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    <span style="background-color: rgba(0, 255, 255, 1)">server {
</span>      <span style="background-color: rgba(0, 255, 255, 1)">server_name yhjmp.test.com;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">location /A-system {
</span>            <span style="background-color: rgba(0, 255, 255, 1)">root /usr/share/nginx/html;
</span>            <span style="background-color: rgba(0, 255, 255, 1)">try_files $uri $uri//A-system/index.html;
</span>            <span style="background-color: rgba(0, 255, 255, 1)">indexindex.html index.htm;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">}
</span>      <span style="background-color: rgba(0, 255, 0, 1)"># 域名不变访问B系统资源
</span>      <span style="background-color: rgba(0, 255, 0, 1)">location/B-system {
</span>            <span style="background-color: rgba(0, 255, 0, 1)">proxy_next_upstream error timeout http_503 http_504 http_502;
</span>            <span style="background-color: rgba(0, 255, 0, 1)">proxy_connect_timeout 500s;
</span>            <span style="background-color: rgba(0, 255, 0, 1)">proxy_read_timeout 500s;
</span>            <span style="background-color: rgba(0, 255, 0, 1)">proxy_send_timeout 500s;<br></span>            <span style="background-color: rgba(255, 0, 0, 1)"># 本次测试做了本地host解析。<br></span>            <span style="background-color: rgba(255, 0, 0, 1)"># 生产环境未做host解析的两套系统需注释proxy_set_header Host $host;这行代码<br></span>            <span style="background-color: rgba(255, 0, 0, 1)">#【proxy_set_header Host $host;含义:会代理到本机主机名下的/B-system目录寻找资源,如果没有则报错404】<br></span>            <span style="background-color: rgba(255, 0, 0, 1)">#【404报错原因:如果客户端请求头中没有携带这个头部,那么传递到后端服务器的请求也不含这个头部。 这种情况下,更好的方式是使用$host变量——它的值在请求包含“Host”请求头时为“Host”字段的值,在请求未携带“Host”请求头时为虚拟主机的主域名】
</span>            <span style="background-color: rgba(255, 0, 0, 1)"># <span style="color: rgba(0, 0, 0, 1)">proxy_set</span><span style="color: rgba(0, 0, 0, 1)">_header Host $host;
</span></span>            <span style="background-color: rgba(0, 255, 0, 1)">proxy_set_header X-Real-IP $remote_addr;
</span>            <span style="background-color: rgba(0, 255, 0, 1)">proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br></span>            <span style="background-color: rgba(255, 255, 0, 1)"># 重写url:(.*)括号面的.* 既是后面的$1。此location中^(.*)$:则是代表匹配url中/B-system/后面的全部内容
</span>            <span style="color: rgba(255, 0, 0, 1)"><span style="background-color: rgba(255, 255, 0, 1)">rewrite^(.*)$   /$1break;#先改写URI地址<br></span><span style="color: rgba(0, 0, 0, 1)">      </span><span style="color: rgba(0, 0, 0, 1)"> </span><span style="color: rgba(0, 0, 0, 1); background-color: rgba(255, 153, 204, 1)"># 反向代理地址:写上域名即可,访问时后面会自行带上/B-system/$1的url</span><span style="color: rgba(0, 0, 0, 1)">
       </span><span style="color: rgba(0, 0, 0, 1); background-color: rgba(255, 153, 204, 1)">proxy_pass <span style="text-decoration: underline">http://yh.test.com</span>;
</span></span>      <span style="background-color: rgba(0, 255, 0, 1)">}
</span>      <span style="background-color: rgba(0, 255, 255, 1)">location / {
</span>          <span style="background-color: rgba(0, 255, 255, 1)">rewrite ^/(.*) <span style="text-decoration: underline">http://yhjmp.test.com/A-system</span> permanent;
</span>      <span style="background-color: rgba(0, 255, 255, 1)">}
</span>    <span style="background-color: rgba(0, 255, 255, 1)">}</span>
}</pre>
</div>
<h2>6、单独访问B系统效果(无法访问到A系统)</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)"><img src="https://img2020.cnblogs.com/blog/1274745/202111/1274745-20211122180052590-984324515.png" alt="" loading="lazy"></span></pre>
<p>&nbsp;</p>
<pre></pre>
<pre></pre>
</div>
<h2>7、使用同一域名访问效果(访问A/B系统)</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)"><img src="https://img2020.cnblogs.com/blog/1274745/202111/1274745-20211122174621226-348083473.png" alt="" loading="lazy"><br><br></span></pre>
</div>
<p>&nbsp;</p>
<p>###</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/faithH/p/15589084.html
頁: [1]
查看完整版本: nginx域名隐性(地址栏域名不变)跳转