前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WSL中部署nginx作为反向代理

WSL中部署nginx作为反向代理

作者头像
drunkdream
发布2020-01-02 15:53:41
2.2K0
发布2020-01-02 15:53:41
举报
文章被收录于专栏:醉梦轩醉梦轩醉梦轩

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 总结

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 前言
  • 0x01 部署nginx
  • 0x02 添加反向代理配置
  • 0x03 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档