nginx是一種反向代理服務器
什麼是反向代理服務器?
正向代理:指客戶端經過下載特定服務器代理軟件,將請求轉發到代理服務器,再轉發到接口服務器
反向代理:指服務端去使用軟件使之扮演客戶端角色,建立一個虛擬的服務器,把真正客戶端的請求經過虛 擬服務器轉發到接口服務器css
因此說nginx是安裝在服務端的一種代理服務器html
博主這裏介紹的安裝方法是在CentOS6.9基礎上:
首先安裝一些編譯軟件和指定庫,CentOS原生系統不能徹底編譯nginxnginx
yum install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel
去官網把.tar.gz
爲後綴,穩定的nginx版本下載到CentOS服務器上c++
wget http://nginx.org/en/download....
tar -xf ...web
編譯而且安裝:vim
#編譯: ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module #安裝: make && make install
如此這般,nginx在服務器上的部署已經完畢
安裝的目錄在/usr/local/nginx
路徑下centos
爲了操做方便,咱們直接配置一些指定命令來啓動和關閉nginx:
第一步,打開init.d下的nginx文件:安全
vim /etc/init.d/nginx
複製下面代碼到nginx文件中,而且:wq
保存:bash
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " $nginxd -s reload #if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`" RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
執行nginx文件:服務器
chmod 755 /etc/init.d/nginx
OK,先咱們能使用service nginx start/stop
來啓動/關閉nginx
若是出現nginx: [emerg] getpwnam(「www」) failed是由於用戶組和用戶問題
輸入一下代碼即可:
/usr/sbin/groupadd -f www /usr/sbin/useradd -g www www
首先查看端口狀況:
netstat -ntlp
看80端口是否被佔用(阿里雲的服務器請打開安全組):
若是80端口占用則殺死該進程,不想殺死進程,就在nginx.conf
裏修改nginx默認端口
nginx的使用都在一個叫nginx.conf的文件中
若是你是按照個人安裝方法安裝,請打開目錄:
/usr/local/nginx/conf
目錄下便會出現nginx.conf
文件,咱們來看下文件中是什麼(代碼cat nginx.conf
):
解釋下:
server表示這裏新建了一個代理服務器
listen表示這個代理服務器監聽的端口是8080(我這裏改過,默認是80端口)
server_name是代理服務器名(若是服務器有域名能夠填寫域名)
location纔是咱們真正須要自定義配置的地方:
root是咱們須要代理的url路徑
好比這裏在location後面寫了/
而後在root裏面寫了html
意思就是當咱們在url中輸入IP:端口/
nginx會幫咱們代理成IP:端口/usr/local/nginx/html/
下面的index表示首頁訪問到index.html
由於/usr/local/nginx/html/路徑下nginx在安裝時建立了一個index.html(你能夠打開目標路徑看下)
因此上面截圖中的server實際的操做結果就是:
訪問IP:8080/index.html
,會出現:
咱們指定服務器的一個端口,而且經過這個端口把服務器部署成文件服務器
依舊是在nginx.conf
文件下,用vi打開。
添加下列代碼:
server { listen 8079; server_name localhost; location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|ttf|woff|woff2|zip)$ { root //picture; } }
每次修改都須要執行命令:service nginx start
很簡單,咱們新建一個名爲localhost的代理服務器
它佔用了8079這個端口
設置了能夠讀取的文件後綴名
而且這些文件是存儲在centos根目錄下的picture文件夾中
這裏我作一個簡單示例:
url:
頁面展現:
tip:若是你們是按個人方法安裝
啓動nginx:service nginx start 中止nginx:service nginx stop
以上即是用nginx搭建一個簡單的文件夾服務器
謝謝你們的閱讀~記得點關注哦