<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>dwt&#039;s life - nginx</title>
<link>https://dwt.life/tag/nginx/</link>
<atom:link href="https://dwt.life/feed/tag/nginx/" rel="self" type="application/rss+xml" />
<language>zh-CN</language>
<description></description>
<lastBuildDate>Mon, 30 Oct 2023 22:56:49 +0800</lastBuildDate>
<pubDate>Mon, 30 Oct 2023 22:56:49 +0800</pubDate>
<item>
<title>MacOS下的openresty编译过程</title>
<link>https://dwt.life/archives/334/</link>
<guid>https://dwt.life/archives/334/</guid>
<pubDate>Mon, 30 Oct 2023 22:56:49 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[其实还是比较简单的，毕竟homebrew已经很方便的安装了依赖，还是记录一下基本依赖我本地的brew是更新过了的，从github下载的最新releasebrew update          ...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>其实还是比较简单的，毕竟<code>homebrew</code>已经很方便的安装了依赖，还是记录一下</p><h2>基本依赖</h2><p>我本地的brew是更新过了的，从github下载的最新release</p><pre><code>brew update                            
brew install pcre openssl</code></pre><h2>configure</h2><p>Linux:</p><pre><code>./configure \                          
   --with-cc-opt=&quot;-I/usr/local/opt/openssl/include/ -I/usr/local/opt/pcre/include/&quot; \
   --with-ld-opt=&quot;-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/&quot; \
   -j8</code></pre><p>MacOS:</p><pre><code>./configure \                     
   --with-cc-opt=&quot;-I/opt/homebrew/include/openssl/ -I/opt/homebrew/include/&quot; \
   --with-ld-opt=&quot;-L/opt/homebrew/lib/&quot; \
   -j8</code></pre><p>这里的路径是我自己找的，brew应该都一样，但是看到别的文章和我的不一样，所以应该先检查include和lib的路径</p><h2>brotli</h2><p>Brotli比我当年编译的简单多了:</p><pre><code>git clone https://github.com/google/ngx_brotli.git`
cd ngx_brotli/deps/brotli
git clone https://github.com/google/brotli.git
cd ..
./configure \                      
   --with-cc-opt=&quot;-I/opt/homebrew/include/openssl/ -I/opt/homebrew/include/&quot; \
   --with-ld-opt=&quot;-L/opt/homebrew/lib/ --add-module=/路径/ngx_brotli&quot; \
   -j8</code></pre><p>然后make -j8 && make install 就到了<code>/usr/local/openresty</code>下了</p>
]]></content:encoded>
<slash:comments>1</slash:comments>
<comments>https://dwt.life/archives/334/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/nginx/</wfw:commentRss>
</item>
<item>
<title>Nginx二级域名自动解析&amp;amp;伪静态&amp;amp;默认首页</title>
<link>https://dwt.life/archives/137/</link>
<guid>https://dwt.life/archives/137/</guid>
<pubDate>Sun, 03 Oct 2021 22:32:00 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[网站的目录结构为：# tree /home/wwwroot/application.pub/home/wwwroot/application.pub├── ai│   └── index.htm...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>网站的目录结构为：</p><pre><code># tree /home/wwwroot/application.pub
/home/wwwroot/application.pub
├── ai
│   └── index.html
└── file
    └── index.html</code></pre><p>/home/wwwroot/application.pub为nginx的安装目录下默认的存放源代码的路径。<br>ai为博客程序源代码路径<br>file为附件路径<br>把相应程序放入上面的路径通过<br><a href="http://ai.application.pub">http://ai.application.pub</a> 访问博客<br><a href="http://file.application.pub">http://file.application.pub</a> 访问附件<br>其它二级域名类推。</p><p>方法一：</p><pre><code>server {
listen 80;
server_name ~^(?&lt;subdomain&gt;.+).application.pub$;
access_log /data/wwwlogs/application.pub_nginx.log combined;
index index.html index.htm index.php;
root /home/wwwroot/application.pub/$subdomain/;
...
}</code></pre><p>方法二：</p><pre><code>server {
listen 80;
server_name *.application.pub;
access_log /home/wwwlogs/application.pub.log combined;
index index.html index.htm index.php;

if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
    set $subdomain $1;
    set $domain $2;
}

location / {
    root /home/wwwroot/application.pub/$subdomain/;
    index index.php $subdomain.html index.htm;
}

...
}</code></pre>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://dwt.life/archives/137/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/nginx/</wfw:commentRss>
</item>
<item>
<title>Nginx /var/run/nginx.pid 问题：nginx: [error] invalid PID number</title>
<link>https://dwt.life/archives/67/</link>
<guid>https://dwt.life/archives/67/</guid>
<pubDate>Wed, 07 Jul 2021 17:39:41 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[根据检查，除了网上说的没有带上-c以外，还有一个情况，是/lib/service里面的nginx.service版本过旧导致的。reload=nginx -s reload即可。]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>根据检查，除了网上说的没有带上-c以外，<br>还有一个情况，是/lib/service里面的nginx.service版本过旧导致的。<br><code>reload=nginx -s reload</code>即可。</p>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://dwt.life/archives/67/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/nginx/</wfw:commentRss>
</item>
<item>
<title>Nginx代理proxy pass配置去除前缀</title>
<link>https://dwt.life/archives/12/</link>
<guid>https://dwt.life/archives/12/</guid>
<pubDate>Tue, 29 Jun 2021 16:45:00 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[使用Nginx做代理的时候，可以简单的直接把请求原封不动的转发给下一个服务。比如，访问abc.com/appv2/a/b.html, 要求转发到localhost:8088/appv2/a/b....]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>使用Nginx做代理的时候，可以简单的直接把请求原封不动的转发给下一个服务。</p><p>比如，访问abc.com/appv2/a/b.html, 要求转发到localhost:8088/appv2/a/b.html</p><p>简单配置如下：</p><pre><code>upstream one {
  server localhost:8088 weight=5;
}

server {
    listen              80;
    server_name         abc.com;
    access_log  &quot;pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G&quot;  main;

    location / {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://one;
    }

}</code></pre><p>即，设置proxy_pass即可。请求只会替换域名。</p><p>但很多时候，我们需要根据url的前缀转发到不同的服务。</p><p>比如</p><p>abc.com/user/profile.html转发到 用户服务localhost:8089/profile.html</p><p>abc.com/order/details.html转发到 订单服务 localhost:8090/details.html</p><p>即，url的前缀对下游的服务是不需要的，除非下游服务添加context-path, 但很多时候我们并不喜欢加这个。如果Nginx转发的时候，把这个前缀去掉就好了。</p><p>一个种方案是proxy_pass后面加根路径/.</p><pre><code>server {
    listen              80;
    server_name         abc.com;
    access_log  &quot;pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G&quot;  main;

    location ^~/user/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://user/;
    }

    location ^~/order/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://order/;
    }

}</code></pre><p>^~/user/表示匹配前缀是user的请求，proxy_pass的结尾有/， 则会把/user/*后面的路径直接拼接到后面，即移除user.</p><p>另一种方案是使用rewrite</p><pre><code>upstream user {
  server localhost:8089 weight=5;
}
upstream order {
  server localhost:8090 weight=5;
}


server {
    listen              80;
    server_name         abc.com;
    access_log  &quot;pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G&quot;  main;

    location ^~/user/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        rewrite ^/user/(.*)$ /$1 break;
        proxy_pass http://user;
    }

    location ^~/order/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        rewrite ^/order/(.*)$ /$1 break;
        proxy_pass http://order;
    }

}</code></pre><p>注意到proxy_pass结尾没有/， rewrite重写了url。</p><p>关于rewrite</p><pre><code>syntax: rewrite regex replacement [flag]
Default: —
Context: server, location, if</code></pre>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://dwt.life/archives/12/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/nginx/</wfw:commentRss>
</item>
</channel>
</rss>