飄過。。。
二,配置nginx
一般nginx配置文件在etc目錄下
cd /etc/nginx
sudo vi nginx.conf
另,如何找nginx.conf配置文件:
sudo find /etc -name nginx.conf //在 /etc 目錄下查找 nginx.conf 配置文件
sudo locate nginx.conf //locate 是在后台數據庫中按文件名搜索 , 搜索速度比 find 更快 , 但對於剛建立的文件 , 使用該命令進行查找將會搜索不到所創建的文件 , 如果想使剛創建的文件被 locate 命令搜索到 , 可以使用 updatedb 命令 , 更新 mlocate 數據庫
在前后端分離端項目里,前端的代碼會被打包成為純靜態文件。使用 Nginx的目的就是讓靜態文件運行起服務,由於后端的接口也是分離的,直接請求可能會產生跨域問題,此時就需要Nginx轉發代理后端接口
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; #PID文件
# Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024; #單個后台worker process進程的最大並發鏈接數 }
http {
gzip on; #開啟gzip壓縮 gzip_min_length 1k; #設置對數據啟用壓縮的最少字節數 gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6; #設置數據的壓縮等級,等級為1-9,壓縮比從小到大 gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; #設置需要壓縮的數據格式 gzip_vary on;
#虛擬主機配置 server {
listen 80;< 大專欄 Nginx部署前后端分離服務/span> server_name mark.binlive.cn; root /home/spa-project/dist; #定義服務器的默認網站根目錄位置 index index.html; #定義index頁面 error_page 404 /index.html; #將404錯誤頁面重定向到index.html可以解決history模式訪問不到頁面問題 location ^~ /api/{ proxy_pass http://127.0.0.1:7000; proxy_send_timeout 1800; proxy_read_timeout 1800; proxy_connect_timeout 1800; client_max_body_size 2048m; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $http_host; # required for docker client's sake proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location ^~ /auth/{ proxy_pass http://127.0.0.1:7000; proxy_send_timeout 1800; proxy_read_timeout 1800; proxy_connect_timeout 1800; client_max_body_size 2048m; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $http_host; # required for docker client's sake proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } }
起到的作用有
-
將前端代碼打包后的dist文件放入指定服務目錄
-
將服務目錄指定到spa-project/dist目錄下即可代理靜態服務
-
配置里開啟了gzip壓縮,可以很大程度上減小文件體積大小
-
將404錯誤頁面重定向到index.html,可以解決前端history路由模式由於刷新頁面訪問不到服務出現404的問題
-
location為代理接口,可以轉發代理后端的請求接口域名或者ip,即可解決接口跨域問題
三,啟動Nginx服務
nginx -t //測試Nginx的配置是否正確
nginx //在配置文件正確的情況下即可啟動nginx服務
nginx -s reload
四,問題
在執行 nginx -t 時出現如下了報錯:
nginx: [emerg] getpwnam("nginx") failed in /etc/nginx/nginx.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
谷歌得到解答,將 user nginx; 替換成 user nobody nogroup; 然后再次執行 nginx -t 就沒有報錯了。
END
Thanks!
https://segmentfault.com/a/1190000014972747
https://serverfault.com/questions/581145/getpwnamwww-failed-in-etc-nginx-nginx-conf
https://blog.csdn.net/tojohnonly/article/details/70160388
