<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nginx &#8211; IT Crafter</title>
	<atom:link href="https://blog.itcrafter.net/archives/tag/nginx/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.itcrafter.net</link>
	<description></description>
	<lastBuildDate>Sat, 29 Jun 2024 09:55:02 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Nginx 用自定义端口反向代理网站服务</title>
		<link>https://blog.itcrafter.net/archives/148</link>
					<comments>https://blog.itcrafter.net/archives/148#respond</comments>
		
		<dc:creator><![CDATA[IT Crafter]]></dc:creator>
		<pubDate>Wed, 06 Mar 2024 17:37:26 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[反向代理]]></category>
		<guid isPermaLink="false">https://blog.itcrafter.net/?p=148</guid>

					<description><![CDATA[在一台服务器上用 Nginx 反向代理多个网站服务是一种常见实践，常见做法是利用服务器上不同的端口来代理不同的 ... <a title="Nginx 用自定义端口反向代理网站服务" class="read-more" href="https://blog.itcrafter.net/archives/148" aria-label="Read more about Nginx 用自定义端口反向代理网站服务">阅读更多</a>]]></description>
										<content:encoded><![CDATA[
<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>在一台服务器上用 Nginx 反向代理多个网站服务是一种常见实践，常见做法是利用服务器上不同的端口来代理不同的网站服务。</p>



<p>但对于一些带有页面重定向的网站服务而言，在页面重定向/页面跳转时会丢失端口信息（比如 Gitlab 登录页面的跳转），进而影响网站服务的正常使用。</p>



<h3 class="wp-block-heading">问题描述</h3>



<p>举个简单例子，比如以下的 URL 反向代理了一套 Gitlab 服务：https://gitlab.mydomain.com:8877</p>



<p>在浏览器地址栏输入以上 URL 后，随即被重定向到：https://gitlab.mydomain.com/users/sign_in</p>



<p>也就是跳转到 https 的默认端口 443，自然无法正常访问到原先在 8877 端口反向代理的 Gitlab 页面：</p>



<figure class="wp-block-image"><img decoding="async" src="https://pic2.helpfully.top:9522/uploads/2024/03/65e8a4077897f.png" alt="image-20240307011240614"/></figure>



<h2 class="wp-block-heading">如何解决</h2>



<p>首先，定位到 Nginx 的配置文件，一般位于 <code>/etc/nginx/nginx.conf</code> 。</p>



<p>然后在配置文件中找到反向代理的配置段，一般是以 <code>location /</code> 标记的内容块。在 <code>proxy_set_header Host</code> 字段中添加 <code>:$server_port</code> 内容，即可在页面重定向时带上自定义端口信息。</p>



<figure class="wp-block-image"><img decoding="async" src="https://pic2.helpfully.top:9522/uploads/2024/03/65e8a71b506a3.png" alt="image-20240307012548193"/></figure>



<p>然后，重启 Nginx，即可顺利跳转到 Gitlab 的登录页面：</p>



<figure class="wp-block-image"><img decoding="async" src="https://pic2.helpfully.top:9522/uploads/2024/03/65e8a7a5dc675.png" alt="image-20240307012807022"/></figure>



<p>OK.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.itcrafter.net/archives/148/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
