nginx安裝和使用
阿新 • • 發佈:2020-12-27
最近做了一個前後端分離的專案,前端使用angular,後端本來用的spring boot, 後來修改成asp.net core,通過nginx部署在linux上。這邊通過實際案例來分享下nginx的部署和使用
安裝不多說,提供一個靠譜的連結:https://www.cnblogs.com/xxoome/p/5866475.html
直接看nginx的配置
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } location /egg { root /etc/nginx/webroot/; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } location /egg/api { proxy_pass http://127.0.0.1:8451/api; } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers PROFILE=SYSTEM; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }
很多地方不用看,主要看location的地方
location /egg代表了前端的程式碼,它是存放在了/etc/nginx/webroot/egg下
location /egg/api代表了後端的程式碼,它是存放在了/etc/nginx/webroot/service下
詳細的安裝步驟如下
1. ng build –prod 編譯angular程式碼,得到dist 2. 編譯java後臺程式碼得到jar檔案 3. 將dist裡面得檔案拷貝到/etc/nginx/webroot/egg裡面,將jar放到/etc/nginx/webroot/server資料夾裡面 4. 修改index.html, 將<basehref ="./">改成<base href ="/egg/"> 5. 配置好nginx.conf,因為前端,後端,資料庫都是在一起,確保angular中“proxy.conf.json”裡面得後臺IP是127.0.0.1,nginx.conf裡面得java程式IP是127.0.0.1,後臺程式中得mangodb得IP是127.0.0.1。
Mongo 配置成自動啟動(目前已配置好),啟動後臺程式。 6. 修改前後端程式時,殺掉java程序,然後把jar放到webroot得server裡面,把dist放到teamzone裡面 後臺終止jar包程式,輸入:ps -ef | grep java,檢視使用java命令的程序,再輸入:kill pid 即可終止執行 後臺執行jar包程式,輸入:nohup java-jar /路徑/程式.jar & 7. 重啟nginx,nginx –s reload
本來proxy_pass中配置的是後臺的ip地址,後來因為載入比較慢,改成了127.0.0.1
當輸入網址http://ip(配置的ip)/egg,nginx會解析該網址,根據egg定位到/etc/nginx/webroot/egg下面的index.html,因為index.html裡面base href是/egg/, 然後跳轉到登入頁面,所以最終的ip呈現為ip/egg/login.
當輸入使用者名稱和密碼,因為後臺已經啟動,傳送http請求給後臺就相當於本地通訊。