1.保證該機器上安裝了nginx
未安裝請看:centos/linux下的安裝Nginx
2.使用root使用者編輯配置檔案
vim /usr/local/nginx/conf/nginx.conf
3.新建vhost資料夾
cd /usr/local/nginx mkdir vhost
4.在檔案中新增這句話(將檔案分類便於管理)
include vhost/*.conf;
5.測試配置檔案
nginx -t
出現如上圖則配置完成
6.在vhost上面新增對應的xxx.conf檔案即可
例如:
需要建立一個lonecloud.cn的二級域名admin.lonecloud.cn將這個二級域名轉發到tomcat上面
建立一個admin.lonecloud.cn.conf
server { default_type 'text/html'; charset utf-8; listen 80; autoindex on; server_name admin.lonecloud.cn; 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 / { proxy_pass http://lonecloud.cn:8080; add_header Access-Control-Allow-Origin *; } }
需要建立一個儲存靜態檔案的二級域名:img.lonecloud.cn
在vhost中建立img.lonecloud.cn.conf
server { listen 80; autoindex off; server_name img.lonecloud.cn; 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 /home/lonecloud/img/; add_header Access-Control-Allow-Origin *; } }
root表示檔案的目錄結構
值得注意的是:
autoindex off;表示是否建立索引,一般設定為off,防止jian將檔案進行窮舉下載