Ubuntu 安裝 Nginx 實現反向代理


安裝Nginx依賴庫(ubuntu平台)

最近域名通過了備案, 想着應用總不能帶着端口號訪問吧, 於是在網上踩了很多坑, 終於找到了一步直達的方法,起碼這一次很順利的實現了

安裝gcc g++的依賴庫

  ubuntu平台:

apt-get install build-essential
apt-get install libtool

  centeros平台:

// centos平台編譯環境使用如下指令
// 安裝make:
yum -y install gcc automake autoconf libtool make
 
// 安裝g++:
yum install gcc gcc-c++  

安裝pcre依賴庫

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安裝zlib依賴庫

apt-get install zlib1g-dev

安裝ssl依賴庫

apt-get install openssl

安裝Nginx

#下載最新版本:
wget http://nginx.org/download/nginx-1.13.8.tar.gz
#解壓:
tar -zxvf nginx-1.13.8.tar.gz
#進入解壓目錄:
cd nginx-1.13.8
#配置:
./configure --prefix=/usr/local/nginx 
#編輯nginx:
make
注意:這里可能會報錯,提示“pcre.h No such file or directory”,具體詳見:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安裝 libpcre3-dev,
命令為:sudo apt-get install libpcre3-dev
#安裝nginx:
sudo make install
#啟動nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,可以通過 -h查看幫助命令。
#查看nginx進程:
ps -ef|grep nginx

  接下來我們可以sudo (super user do) 一下看是否安裝正常: (注意: 關注一下目錄路徑!!!)

poerchant@ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  如果顯示如上, 則表示配置文件正確. 否則會有相關的提示

 

  接下來就是ip映射的關鍵步驟了: 

# 進入nginx配置目錄文件夾下
root@iZuf6hcl8bs46q83p5v42hZ:~# cd /usr/local/nginx/conf/
# ls 查看文件你會看見一個 nginx.conf的文件
# 進入nginx.conf文件並編輯 
root@iZuf6hcl8bs46q83p5v42hZ:/usr/local/nginx/conf#  vi nginx.conf


# 你會找到這樣的一部分

#設定虛擬主機配置
    server {
        #偵聽80端口
        listen    80;
        #定義使用 www.nginx.cn訪問
        server_name  www.nginx.cn;
 
        #定義服務器的默認網站根目錄位置
        root html;
 
        #設定本虛擬主機的訪問日志
        access_log  logs/nginx.access.log  main;
 
        #默認請求
        location / {
            
            #定義首頁索引文件的名稱
            index index.php index.html index.htm;   
 
        }
 
        # 定義錯誤提示頁面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }
 
        #靜態文件,nginx自己處理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #過期30天,靜態文件不怎么更新,過期可以設大一點,
            #如果頻繁更新,則可以設置得小一點。
            expires 30d;
        }
 
        #PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
 
        #禁止訪問 .htxxx 文件
            location ~ /.ht {
            deny all;
        }
 
    }

  以上代碼實在太多了, 如果你和我一樣只是前端代碼可以再簡化一下如下: 

(

編輯文件: i

編輯完成后 esc

之后 shift + : wq

)

# 默認的配置
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

# 新添加的配置(如果有需要可以再添加)
server {
        listen 80;
        server_name chat.sinbada.top; # 你的域名地址

        location / {
            index index.html;
            proxy_pass http://101.132.69.201:8888; # 你的項目ip地址
        }
}

  OK, 至此所有步驟基本完成, 最后一步就是重啟nginx

nginx -s reload     重新載入nginx(當配置信息發生修改時)  

  大功告成!!!

  直接打開域名就可以訪問項目了, 完美!

 

Nginx常用命令

  啟動Nginx

nginx常用命令  
    nginx -c /usr/local/nginx/conf/nginx.conf  啟動nginx(windows下start nginx);  
    nginx -s quit       停止ngix  
    nginx -s reload     重新載入nginx(當配置信息發生修改時)  
    nginx -s reopen     打開日志文件  
    nginx -v            查看版本  
    nginx -t            查看nginx的配置文件的目錄  
    nginx -h            查看幫助信息  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM