1.介紹
Nginx是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,其特點是占有記憶體少,并發能力強,事實上nginx的并發能力在同型別的網頁服務器中表現較好,中國大陸使用nginx的網站有:百度、京東、新浪、網易、騰訊、淘寶等,
Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發布于2004年10月4日,
官網:https://nginx.org/
2.Nginx-命令(手動安裝限定)
1.查看版本
./nginx -v
2.檢查組態檔
./nginx -v
3.啟動
./nginx
4.停止
./nginx -s stop
停止之后,我們可以查看nginx的行程:
ps -ef|grep nginx
5.重新加載
當修改了Nginx組態檔后,需要重新加載才能生效,可以使用下面命令重新加載組態檔:
./nginx -s reload
3.環境變數配置
在上述我們在使用nginx命令在進行服務的啟動、停止、重新加載時,都需要用到一個指令nginx,而這個指令是在nginx/sbin目錄下的,我們每一次使用這個指令都需要切換到sbin目錄才可以,使用相對繁瑣,那么我們能不能在任意目錄下都可以執行該指令來操作nginx呢?答案是可以的,配置nginx的環境變數即可,
打開/etc/profile檔案, 在PATH環境變數中增加nginx的sbin目錄,如下:
ecpot PATH=/usr/local/nginx/sbin:$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
修改完組態檔之后,需要執行 source /etc/profile 使檔案生效, 接下來,我們就可以在任意目錄下執行nginx的指令了
#安裝
手動安裝
1.安裝依賴包
由于nginx是基于c語言開發的,所以需要安裝c語言的編譯環境,及正則運算式庫等第三方依賴庫,
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
2.下載Nginx安裝包
以1.16.1版本為例
yum install wget
wget https://nginx.org/download/nginx-1.16.1.tar.gz
wget:
wget命令用來從指定的URL下載檔案,wget非常穩定,它在帶寬很窄的情況下和不穩定網路中有很強的適應性,如果是由于網路的原因下載失敗,wget會不斷的嘗試,直到整個檔案下載完畢,如果是服務器打斷下載程序,它會再次聯到服務器上從停止的地方繼續下載,
3.解壓nginx壓縮包
tar -zxvf nginx-1.16.1.tar.gz
4.配置Nginx編譯環境
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx
----prefix 指定的目錄,就是我們安裝Nginx的目錄,
5.編譯&安裝
make & make install
docker安裝
1.docker拉取nginx鏡像
docker pull nginx
2.創建映射容器的檔案目錄
創建組態檔目錄
mkdir -p /mydata/nginx/conf/
mkdir -p /mydata/nginx/conf.d/
mkdir -p /mydata/nginx/log/
授予權限
chmod 777 /mydata/nginx/conf/
chmod 777 /mydata/nginx/conf.d/
chmod 777 /mydata/nginx/log/
3.在/mydata/nginx/conf/中創建nginx.conf檔案
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
4.在/mydata/nginx/conf.d/中創建default.conf檔案
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
5.創建容器
docker run -p 80:80 --name nginx \
--restart=always \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-v /mydata/nginx/log:/var/log/nginx \
-d nginx
-d:后臺運行
-p:埠映射
--name:指定容器名稱
-v:資料卷映射
--restart:重啟容器方式
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543371.html
標籤:Java
上一篇:Java中JWT的使用
