WSL中部署nginx作为反向代理

0x00 前言

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

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

0x01 部署nginx

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

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

  1. $ apt install nginx -y
COPY

由于默认配置监听的是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服务:

  1. service nginx start
COPY

注意:不能使用systemctl命令。

0x02 添加反向代理配置

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

  1. ##
  2. # You should look at the following URL's in order to grasp a solid understanding
  3. # of Nginx configuration files in order to fully unleash the power of Nginx.
  4. # https://www.nginx.com/resources/wiki/start/
  5. # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
  6. # https://wiki.debian.org/Nginx/DirectoryStructure
  7. #
  8. # In most cases, administrators will remove this file from sites-enabled/ and
  9. # leave it as reference inside of sites-available where it will continue to be
  10. # updated by the nginx packaging team.
  11. #
  12. # This file will automatically load configuration files provided by other
  13. # applications, such as Drupal or Wordpress. These applications will be made
  14. # available underneath a path with that package name, such as /drupal8.
  15. #
  16. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
  17. ##
  18. # Default server configuration
  19. #
  20. server {
  21. listen 127.0.0.1:80;
  22. # listen [::]:80 default_server;
  23. # SSL configuration
  24. #
  25. # listen 443 ssl default_server;
  26. # listen [::]:443 ssl default_server;
  27. #
  28. # Note: You should disable gzip for SSL traffic.
  29. # See: https://bugs.debian.org/773332
  30. #
  31. # Read up on ssl_ciphers to ensure a secure configuration.
  32. # See: https://bugs.debian.org/765782
  33. #
  34. # Self signed certs generated by the ssl-cert package
  35. # Don't use them in a production server!
  36. #
  37. # include snippets/snakeoil.conf;
  38. root /var/www/html;
  39. # Add index.php to the list if you are using PHP
  40. index index.html index.htm index.nginx-debian.html;
  41. server_name service.com; # 此处替换为对应服务的域名,可以通过配置hosts文件来指向127.0.0.1
  42. location / {
  43. proxy_pass http://127.0.0.1:8000;
  44. proxy_set_header Host $host;
  45. proxy_set_header X-Real-IP $remote_addr;
  46. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  47. proxy_set_header X-Forwarded-Proto $scheme;
  48. }
  49. # pass PHP scripts to FastCGI server
  50. #
  51. #location ~ \.php$ {
  52. # include snippets/fastcgi-php.conf;
  53. #
  54. # # With php-fpm (or other unix sockets):
  55. # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  56. # # With php-cgi (or other tcp sockets):
  57. # fastcgi_pass 127.0.0.1:9000;
  58. #}
  59. # deny access to .htaccess files, if Apache's document root
  60. # concurs with nginx's one
  61. #
  62. #location ~ /\.ht {
  63. # deny all;
  64. #}
  65. }
COPY

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

  1. $ service nginx reload
COPY

0x03 nginx常用配置方法

  1. location /api/ {
  2. proxy_pass http://127.0.0.1:8000;
  3. }
COPY

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

  1. location /api/ {
  2. proxy_pass http://127.0.0.1:8000/;
  3. }
COPY

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

  1. location = / {
  2. }
COPY

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

  1. location ^~ /images/ {
  2. }
COPY

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

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

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

  1. location / {
  2. }
COPY

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

0x04 总结

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

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

分享
0 comments
Anonymous
Markdown is supported

Be the first guy leaving a comment!