nginx反向代理的伺服器安裝及域名解析配置(Linux)
阿新 • • 發佈:2018-12-23
簡介
nginx是一款輕量級的web伺服器,也是一款反向代理伺服器(域名轉發就是反向代理的功能)
1.nginx可以直接支援rails和php的程式
2.可以作為HTTP的反向代理伺服器
3.作為負載均衡伺服器
4.作為郵件代理伺服器
5.幫助前端實現動靜分離
特點 : 高穩定,高效能,資源佔用少,功能豐富並支援很多外掛,模組化的維護,支援熱部署
一、安裝:
1.安裝依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.下載安裝包
在nginx.org中可以下載想要的版本,$table version是穩定版本,下這個版本就行
tar -zxvf 你下好的tar包
進入你的nginx的壓縮目錄下,準備開始安裝
cd nginx-1.10.2
./configure
make
make install
進入nginx的安裝目錄
whereis nginx
cd /usr/local/nginx
cd /sbin
./nginx
nginx就已經安裝啟動好了,預設為80埠,輸入localhost就可以看到nginx頁面了
二、域名解析配置
進入ngxin的安裝目錄
cd /usr/local/nginx
cd conf
建立vhost資料夾,用來存放各個域名的conf檔案,這樣做是為了更方便管理各個域名的解析,否則都寫在nginx.conf下,隨著時間的發展,內容會越來越多,不好管理
mkdir vhost
vim nginx.conf
在HTTPs server上的空白處insert
include vhost/*.conf;
儲存退出
編寫一個配置檔案 admin.happymmall.com.conf
server {
listen 80; #監聽的是80埠
#自動建立索引,如果為off,那麼nginx就會給首頁403錯誤,但要是知道了檔案的具體路徑,那麼還是可以訪問的
#意思是你訪問的時候,會把資料夾下的東西以目錄的形式像你展示,例如下載軟體的伺服器,他就會有各種的軟體的目錄,當然對於一些js檔案什麼的,我們不想讓別人看見,就會設定成off
autoindex on;
server_name admin.happymmall.com; #線上的域名
access_log /usr/local/nginx/logs/access.log combined;
#預設不輸入其他東西,開啟首頁的順序
index index.html index.htm index.jsp index.php;
#下面的兩個不重要,因為都在location下配置了
#root /devsoft/apache-tomcat-7.0.73/webapps/mmall;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\ >].*" ){
return 404;
}
#主要的是location
#轉發到線上的一個資料夾中
location = / {
root /product/front/mmall_admin_fe/dist/view;
index index.html;
}
location ~ .*\.(html|htm)$ {
root /product/front/mmall_admin_fe/dist/view;
index index.html;
}
#如果輸入了線上的域名,那麼就會跳轉到tomcat上
location / {
proxy_pass http://127.0.0.1:8080/;
add_header Access-Control-Allow-Origin '*';
}
}
編寫一個第二種配置檔案,跳轉到資料夾的例子 img.happymmall.com.conf
server {
listen 80;
autoindex off;
server_name img.happymmall.com;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
deny all;
}
#輸入配置檔案上的域名,就會跳轉到資料夾
location / {
root /product/ftpfile/img/;
add_header Access-Control-Allow-Origin *;
}
}
建立編輯好後,要重啟nginx生效
./nginx -s reload