1.安裝依賴
#gcc安裝,nginx源碼編譯需要 yum install gcc-c++ #PCRE pcre-devel 安裝,nginx 的 http 模塊使用 pcre 來解析正則表達式 yum install -y pcre pcre-devel #zlib安裝,nginx 使用zlib對http包的內容進行gzip yum install -y zlib zlib-devel #OpenSSL 安裝,強大的安全套接字層密碼庫,nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http) yum install -y openssl openssl-devel
2.下載
#下載版本號可根據目前官網最新穩定版自行調整 wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
3.安裝
#根目錄使用ls命令可以看到下載的nginx壓縮包,然后解壓 tar -zxvf nginx-1.16.1.tar.gz #解壓后進入目錄 cd nginx-1.16.1 #使用默認配置 ./configure #編譯安裝 make make install #查找安裝路徑,默認都是這個路徑 [root@VM_0_12_centos ~]# whereis nginx nginx: /usr/local/nginx #啟動、停止nginx cd /usr/local/nginx/sbin/ ./nginx #啟動 ./nginx -s stop #停止,直接查找nginx進程id再使用kill命令強制殺掉進程 ./nginx -s quit #退出停止,等待nginx進程處理完任務再進行停止 ./nginx -s reload #重新加載配置文件,修改nginx.conf后使用該命令,新配置即可生效 #重啟nginx,建議先停止,再啟動 ./nginx -s stop ./nginx #查看nginx進程,如下返回,即為成功 [root@VM_0_12_centos ~]# ps aux|grep nginx root 5984 0.0 0.0 112708 976 pts/1 R+ 14:41 0:00 grep --color=auto nginx root 18198 0.0 0.0 20552 612 ? Ss 11:28 0:00 nginx: master process ./nginx nobody 18199 0.0 0.0 23088 1632 ? S 11:28 0:00 nginx: worker process
直接訪問服務器的IP,如果出現以下畫面說明成功
4.配置開機自啟動
如果所用工具能直接打開文件,則可以直接打開修改,不能的話用vi修改
#在rc.local增加啟動代碼即可 vi /etc/rc.local #增加一行 /usr/local/nginx/sbin/nginx,增加后保存 #設置執行權限 cd /etc chmod 755 rc.local
5.配置映射
默認配置是80端口,localhost,執行Nginx root目錄下的html文件,如果要配多個location,並指向別的文件可參考如下
location / { root html; index index.html index.htm; } location /auth { alias /home/webapps/ruqi-web-auth/dist; index index.html index.html; } location /order { alias /home/webapps/ruqi-web-order/dist; index index.html index.html; }
alias是指指向別的目錄,設置完,指向命令
./nginx -s reload
就可通過ip/auth,或IP/order,訪問相應的項目
6.反向代理配置解決跨域問題
前后端分離總是會遇到跨域問題,最后的解決辦法無非是后端設置允許跨域訪問,或者中間用Nginx或noodle作代理
接口用統一的一個前綴,例如"api",則可添加如下配置
location /api/ { rewrite ^.+/api/?(.*)$ /$1 break; include uwsgi_params; proxy_pass http://172.16.205.107:8085/ruqi-auth/; }
意思是所有api接口都會代理到 172.16.205.107:8085/ruqi-auth/ 服務器
7.內容參考
centos7安裝Nginx、使用nginx記錄 - 夜的隱為者 - OSCHINA
如果沒有Linux環境,也可以在Windows上安裝,只是命令不一樣
我們不生產代碼,我們只是代碼搬運工Y(^_^)Y