WSL中部署nginx作为反向代理

0x00 前言

自从有了WSL,在Windows上部署各种服务也变得更加方便。最近,遇到一个问题,本地调试一些Web服务的时候,必须要使用80端口,如果同时有两个服务都监听80端口必然会导致冲突。而且,在Linux中监听80端口需要使用root权限,每次启动的时候都要加上sudo也挺麻烦的。

因此,想到可以利用nginx的反向代理能力,在本地进行HTTP的转发,这样,每个服务就可以使用自己的端口了。

0x01 部署nginx

WSL上部署nginx,和Linux下上是基本一致的。

我的WSL中安装的是Ubuntu 18.04,以下以该系统为例。

$ apt install nginx -y

由于默认配置监听的是0.0.0.0地址,而我们只是本地使用,需要改成127.0.0.1

修改/etc/nginx/sites-available/default文件中的listen 80 default_server;listen 127.0.0.1:80 default_server;,并注释掉listen [::]:80 default_server;(不需要开启ipv6)。

启动nginx服务:

service nginx start

注意:不能使用systemctl命令。

0x02 添加反向代理配置

/etc/nginx/conf.d目录中添加一个配置文件service.conf,内容如下:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 127.0.0.1:80;
    # listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name service.com; # 此处替换为对应服务的域名,可以通过配置hosts文件来指向127.0.0.1

    location / {
        proxy_pass http://127.0.0.1:8000;
        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-Forwarded-Proto  $scheme;
     }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #    include snippets/fastcgi-php.conf;
    #
    #    # With php-fpm (or other unix sockets):
    #    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #    # With php-cgi (or other tcp sockets):
    #    fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}

}

每个服务添加一个对应的.conf文件,然后重新加载nginx服务:

$ service nginx reload

0x03 nginx常用配置方法

    location /api/ {
        proxy_pass http://127.0.0.1:8000;
     }

这种方法表示转发整个路径,如:/api/test/会转发到http://127.0.0.1:8000/api/test/

    location /api/ {
        proxy_pass http://127.0.0.1:8000/;
     }

这种方法表示转发/api/后的路径,如:/api/test/会转发到http://127.0.0.1:8000/test/

location = / {
}

这种表示完全匹配,具有最高优先级

location ^~ /images/ {
}

这种表示匹配所有以/images/开头的url,优先级仅次于=

location ~*.(gif|jpg|jpeg)$ {
}

使用正则方式匹配,这里表示所有以gifjpgjpeg结尾的请求

location / {
}

匹配任意请求,优先级最低

0x04 总结

WSL极大简化了在Windows上部署各种服务的流程,同时,也可以学习Linux上的常用操作,可谓一举两得。

自从有了WSL,Windows也变得从未如此友好!

分享