﻿# 如何在強制使用代理伺服器的環境下設定 git, npm, bower, gem, ionic 工具

有些公司會要求員工上網時必須設定代理伺服器 (Proxy Server) ，在這樣的環境下使用這些前端工具經常會遇到許多網路連線的問題，本篇文章將說明如何針對 git, npm, bower, gem, ionic 等工具進行完整的代理伺服器設定，設定好之後就可以大幅漸少這些奇奇怪怪的問題。

我們先**假設**代理伺服器的位址為：

-   **http://10.0.0.1:8080**

### 設定 Git 使用代理伺服器

輸入兩行指令即可設定完畢：

-   git config --global https.proxy **http://10.0.0.1:8080**
-   git config --global http.proxy **http://10.0.0.1:8080**
-   git config --global http.sslVerify false

取消設定 Proxy 的方法：

-   git config --global --unset http.proxy
-   git config --global --unset https.proxy
-   git config --global --unset http.sslVerify

**參考資料**

-   [Git - git-config Documentation](http://git-scm.com/docs/git-config)

### 設定 npm 使用代理伺服器

輸入兩行指令即可設定完畢：

-   npm config set https-proxy **http://10.0.0.1:8080**
-   npm config set proxy **http://10.0.0.1:8080**
-   npm config set strict-ssl false

取消設定 Proxy 的方法：

-   npm config delete https-proxy
-   npm config delete proxy
-   npm config delete strict-ssl

**參考資料**

-   [config | npm Documentation](https://docs.npmjs.com/misc/config)
-   [node.js - How to install NPM behind authentication proxy on Windows? - Super User](https://superuser.com/questions/347476/how-to-install-npm-behind-authentication-proxy-on-windows)

### 設定 Bower 使用代理伺服器

請建立一個 **%UserProfile%\\.bowerrc** 檔案 ( 若為 Mac OS X 請建立 **~/.bowerrc** 檔案 )，其內容如下：

```
{
  "proxy":       "http://10.0.0.1:8080",
  "https-proxy": "http://10.0.0.1:8080",
  "strict-ssl":  false
}
```

取消設定 Proxy 只要將 **%UserProfile%\\.bowerrc** 檔案刪除即可。

**參考資料**

-   [Configuration · Bower](http://bower.io/docs/config/)

### 設定 Ruby Gems 使用代理伺服器

只要設定 **http\_proxy** 與 **https\_proxy** 環境變數即可。

#### Windows 環境下

-   目前工作階段的環境變數
    -   設定
        -   set http\_proxy=**http://10.0.0.1:8080**
        -   set https\_proxy=**http://10.0.0.1:8080**
    -   刪除設定
        -   set http\_proxy=
        -   set https\_proxy=
-   設定使用者環境變數
    -   設定
        -   setx http\_proxy **http://10.0.0.1:8080**
        -   setx https\_proxy **http://10.0.0.1:8080**
    -   刪除設定
        -   setx http\_proxy ""
        -   setx https\_proxy ""
-   設定全域系統環境變數
    -   設定
        -   setx http\_proxy **http://10.0.0.1:8080** /M
        -   setx https\_proxy **http://10.0.0.1:8080** /M
    -   刪除設定
        -   setx http\_proxy "" /M
        -   setx https\_proxy "" /M

#### Mac OS X 或 Linux 環境下

-   目前工作階段的環境變數
    -   設定
        -   export http\_proxy=**http://10.0.0.1:8080**
        -   export https\_proxy=**http://10.0.0.1:8080**
    -   刪除設定
        -   unset http\_proxy
        -   unset https\_proxy
-   設定使用者環境變數
    -   將 export 指令寫入 **~/.bashrc** 或 **~/.bash\_profile** 即可

**參考資料**

-   [Command Reference - RubyGems Guides](http://guides.rubygems.org/command-reference/#gem-environment)

### 設定 Ionic 使用代理伺服器

只要設定 **PROXY** 環境變數即可。

#### Windows 環境下

-   目前沒找到解決方法，設定了 PROXY 環境變數後，執行 ionic start 命令會發生錯誤，目前 ionic 這個版本似乎有點問題，且目前網路上找到的方法都不能用。

#### Mac OS X 或 Linux 環境下

-   目前工作階段的環境變數
    -   設定
        -   export PROXY\=**http://10.0.0.1:8080**
    -   刪除設定
        -   unset PROXY
-   設定使用者環境變數
    -   將 export 指令寫入 **~/.bashrc** 或 **~/.bash\_profile** 即可
