乐闻世界logo
搜索文章和话题

How to exclude certain domains from an npm proxy

1个答案

1

When using npm, you may encounter a situation where you need to access the npm registry through a proxy server, but you also want certain specific domains to bypass the proxy directly. This requirement is particularly common when working with private npm repositories in enterprise environments. Below is how to exclude specific domains from npm's proxy settings:

  1. Set npm Proxy First, configure the npm proxy using the npm config command. For example, if your proxy server address is http://proxy.example.com:8080, set the HTTP and HTTPS proxies as follows:

    bash
    npm config set proxy http://proxy.example.com:8080 npm config set https-proxy http://proxy.example.com:8080
  2. Use no_proxy to Exclude Specific Domains To exclude certain domains from the proxy, utilize the no_proxy environment variable. This variable specifies domains that should bypass the proxy. For instance, if you want example.org and myprivate.repo to access directly without the proxy, set:

    bash
    npm config set no-proxy example.org,myprivate.repo

    Alternatively, configure it directly in your environment variables:

    bash
    export NO_PROXY=example.org,myprivate.repo

    On Windows, add the NO_PROXY variable through the environment variable settings.

  3. Verify Configuration After setting up, confirm your configuration by checking npm's settings:

    bash
    npm config list

    This command displays all npm configuration details, including your proxy settings and excluded domains.

  4. Example Scenario Consider an enterprise environment with a private npm repository at myprivate.repo and the need to access the public npm registry. The company's network policy mandates that all external access must go through a proxy. Here, configure the npm proxy and add myprivate.repo to no_proxy to ensure direct access to the private repository, maintaining both speed and security.

With this setup, you can access external resources via the proxy while directly connecting to specific internal or sensitive domains, satisfying security policies and optimizing network efficiency.

2024年6月29日 12:07 回复

你的答案