目錄
一、安裝Nginx
1、安裝完成
2、啟動(dòng)Nginx
3、問題
二、修改Nginx配置文件
系統(tǒng)環(huán)境:Mac Pro—10.15.7版本
Nginx版本:1.19.6
一、安裝Nginx
brew install nginx
1、安裝完成
Nginx的配置文件目錄:/usr/local/etc/nginx
Nginx的安裝目錄:/usr/local/Cellar/nginx
2、啟動(dòng)Nginx
brew services start nginx
3、問題
可能遇到的報(bào)錯(cuò)一:nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
解決:原因是默認(rèn)nginx主配置文件位置與你當(dāng)前不一樣。使用終端進(jìn)入到Nginx安裝位置,然后輸入 【./nginx -h】 即可查看
?找到-e filename那一欄,將default后面的路徑復(fù)制下來,然后使用命令指定nginx.conf文件的位置:
【./nginx -c /usr/local/etc/nginx/nginx.conf】,之后輸入命令重新加載:【./nginx -s reload】即可。
二、修改Nginx配置文件
打開Nginx配置文件:/usr/local/etc/nginx/nginx.conf
需要修改3處,下面的配置文件中已添加注釋,查看“注意”開頭的說明
- root后面的前端打包好的文件路徑;
- location后面的映射路徑,該路徑為服務(wù)端Swagger配置的pathMapping值;
- proxy_pass后面的本機(jī)ip地址;
#user nobody;
worker_processes 1;
#pid logs/nginx.pid;
pid /usr/local/Cellar/nginx/1.19.6/logs/nginx.pid;
events {
worker_connections 1024;
}
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 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
#注意:修改root后面的地址為你打包好的dist文件夾前端項(xiàng)目路徑
location / {
root xx/yy/zz/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
#注意:/prod-api/ 為你的服務(wù)端Swagger中配置的pathMapping屬性值
#注意:修改proxy_pass后面的地址為你部署機(jī)器的ip地址,切記端口號(hào)后面斜杠不要去掉【/】
location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://0.0.0.0:8081/;
}
#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;
#}
#省略.......
}
include servers/*;
}
配置好后輸入命令重新加載【./nginx -s reload】。文章來源:http://www.zghlxwxcb.cn/news/detail-502295.html
之后在瀏覽器輸入本機(jī)ip地址即可訪問,注意:如果服務(wù)端的端口號(hào)為80開頭,ip地址后面不加端口號(hào)。文章來源地址http://www.zghlxwxcb.cn/news/detail-502295.html
到了這里,關(guān)于Nginx部署Vue前端項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!