nginx基礎安裝和相關配置之一
安裝
下載與啟動
Linux安裝nginx
在linux下安裝nginx,首先需要安裝 gcc-c++編譯器。然後安裝nginx依賴的pcre和zlib包。最後安裝nginx即可。
1.先安裝gcc-c++編譯器
yum install gcc-c++
yum install -y openssl openssl-devel
2.再安裝pcre包
yum install -y pcre pcre-devel
3.再安裝zlib包
yum install -y zlib zlib-devel
4.下載nginx壓縮包(https://nginx.org/download/)
wget https://nginx.org/download/nginx-1.19.9.tar.gz
5.解壓並進入nginx目錄
tar -zxvf nginx-1.19.9.tar.gz
cd nginx-1.19.9
4.使用nginx預設配置
./configure
5.編譯安裝
make
make install
6.查詢安裝路徑
whereis nginx
7.進入sbin目錄,可以看到有一個可執行檔案nginx,直接./nginx執行就OK了。
./nginx
9.檢視是否啟動成功
ps -ef | grep nginx
10.然後在網頁上訪問自己的IP就可以了預設埠為80(出現如下歡迎介面就成功了!)
防火牆
https://blog.csdn.net/realjh/article/details/82048492
開放埠
檢視防火牆是否開啟 systemctl status firewalld
啟動防火牆後,預設沒有開啟任何埠,需要手動開啟埠。nginx預設是80埠
手動開啟埠命令
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含義: --zone #作用域 --add-port=80/tcp #新增埠,格式為:埠/通訊協議 --permanent #永久生效,沒有此引數重啟後失效
開啟後需要重啟防火牆才生效
systemctl restart firewalld.service
檢視防火牆是否開啟了80埠的訪問
firewall-cmd --list-all
端口占用
關停現有被佔用埠(慎用)
如果啟動後出現瞭如下的問題就是80埠被佔用
可以用下面這個命令進行檢視80埠被誰佔用
netstat -tunlp | grep 80
這裡因為我之前開啟了的是被nginx.master或者nginx.woeker佔用就不用管,如果不是這個的話那就把那個程序關閉掉
kill -9 程序號
關閉之後重啟nginx再次訪問!!
進入sbin目錄,可以看到有一個可執行檔案nginx,直接./nginx reload
./nginx reload
修改nginx conf檔案配置
- 防火牆開放新修改的埠(與nginx conf中修改後的listen保持一致)
檢視想開的埠是否已開:
firewall-cmd --query-port=9527/tcp
新增指定需要開放的埠:
firewall-cmd --add-port=9527/tcp --permanent
過載入新增的埠:
firewall-cmd --reload
查詢指定埠是否開啟成功:
firewall-cmd --query-port=9527/tcp
- 修改後重啟
cd /usr/local/nginx/sbin
./nginx reload
配置反向代理
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
accept_mutex on; #設定網路連線序列話,防止驚群現象,預設為on
multi_accept on; #設定一個程序是否同時接受多個網路連線,預設為off
worker_connections 1024; #最大連線數,預設為512
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 9527;
server_name 127.0.0.1;
#charset koi8-r;
charset utf-8;
access_log logs/host.access.log;
error_log logs/host.error.log;
keepalive_requests 5;
location / {
root /usr/local/labelSys/frontend;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /antitf-boot {
proxy_pass http://127.0.0.1:8090/labelSys;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
history路由模式下部署,重新整理404
- 原因是因為web單頁面開發模式,只有一個index.html入口,其他路徑是前端路由去跳轉的,nginx沒有對應這個路徑,當然就是404了。
- 一般nginx監聽配置如下
location / {
root /mydata/transfer/html/helper/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
在配置中加上try_files,意為,“嘗試讀取檔案”。u r i 這 個 是 n g i n x 的 一 個 變 量 , 存 放 著 用 戶 訪 問 的 地 址 , 例 如 h t t p : / / l o c a l h o s t : 9527/ c h o o s e S i z e
- uri這個是nginx的一個變數,例如http://localhost:8200/chooseSize,那麼uri就是/chooseSize;
- uri/ 代表訪問的是一個目錄 例如http://localhost:8200/chooseSize/ ,那麼uri/就是/chooseSize/;
- /index.html就是我們首頁的地址
負載多服務
https://blog.csdn.net/weixin_43442246/article/details/109721517