Nginx 簡單使用(極簡介紹)

介紹

使用Nginx不難,但是如果你剛開始接觸,需要花點時間才能理解Nginx到底是個什么意思,而相應的反向代理又是什么意思?

知乎上看到一個圖,簡單明了
Paste_Image.png

使用方法(ubuntu環境)

  1. 安裝
    $ apt-get install nginx

  2. 主要配置文件

    安裝后 目錄在/etc/nginx
    一般只需修改/etc/nginx/sites-available/default文件 就行了
    日志文件在 /var/log/nginx

  3. 配置及使用
    不使用nginx的時候 假設有兩個程序,地址分別為
    http://127.0.0.1:8080
    http://127.0.0.1:8082

server {
    listen 80;
    server_name a.com;
    charset utf-8;
    access_log  /home/a.com.access.log;

    location /(css|js|img)/ {
        access_log off;
        expires 1d;
        root “/home/rick/static";
        try_files $uri @backend;
    }

    location / {
        try_files /_not_exists_ @backend;
    }

    location @backend {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            $http_host;
        proxy_pass http://127.0.0.1:8080;
    }
}

server {
    listen 80;
    server_name b.com;
    charset utf-8;
    access_log  /home/a.com.access.log;

    location /(css|js|img)/ {
        access_log off;
        expires 1d;
        root “/home/rick/static";
        try_files $uri @backend;
    }

    location / {
        try_files /_not_exists_ @backend;
    }

    location @backend {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            $http_host;
        proxy_pass http://127.0.0.1:8082;
    }
}
  1. 啟動
    $ service nginx start //reload restart stop
    設置完后,訪問a.com 和b.com 就分別訪問了8080 和8081
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容