一、安装brew
// 如遇到raw.githubusercontent.com连接失败问题
// 找到对应的hosts文件
// 1. sudo vi /etc/hosts
// 2. i 回车编辑
// 3. 199.232.28.133 raw.githubusercontent.com
// 4. :wq! 强制保存
// 输入以下命令,下载
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
注意:如遇到,下载完成后,提示zsh: command not found: brew 运行以下操作:(路径可替换,自定义)
- 执行以下命令: echo “export PATH=$PATH:/opt/homebrew/bin” >> ~/.zprofile
- 执行命令:export PATH=$PATH:/opt/homebrew/bin
- 最后执行:source ~/.zprofile
二、安装nginx
- 安装nginx
// 安装
brew install nginx
// 默认安装位置
/opt/homebrew/etc/nginx
// 查看nginx的配置信息
brew info nginx
// 打开相关目录
open /opt/homebrew/etc/nginx
// host文件
/private/etc/hosts
-
打开访达,command+shift+g
-
输入终端相关nginx路径 /opt/homebrew/etc/nginx
-
nginx常用命令
sudo nginx -s stop
nginx -s reload 重新加载配置
nginx -s reopen 重启
nginx -s stop 停止
nginx -s quit 退出
nginx -V 查看版本,以及配置文件地址
nginx -v 查看版本
nginx -c filename 指定配置文件
nginx -h 帮助
nginx -t 查看配置项有没有配置错误
三、nginx配置
-
如上找到相关nginx配置文件
-
将前端vue/react打包后文件夹dist/build,放入nginx文件夹内
-
修改nginx.conf 配置文件
-
看端口是否被占用
// 查看端口是否被占用
netstat -anvp tcp |grep 10001
// 杀掉进程
killall -9 nginx
- 启动nginx
sudo nginx
四、配置项参考(持续补充中)
user admin;
worker_processes 1;
#定位全局错误日志文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定进程id的存储文件位置
#pid 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;
# 监听地址,可以是ip,最好是域名
server_name localhost;
allow all;
deny 10.0.1.81;
#index index.html index.htm index.php;
#站点目录
#root /opt/homebrew/etc/nginx/dist/html;
# 设置访问的语言编码
#charset koi8-r;
#access_log logs/host.access.log main;
#add_header Content-Security-Policy "upgrade-insecure-requests;connect-src *";
# 前端访问地址
location / {
# FTP 目录的绝对路径, 如若采用 alias 必须以`/` 结尾
alias /opt/homebrew/etc/nginx/dist/;
index index.html index.htm;
try_files $uri $uri/ /manager/index.htm;
}
location /admin {
#add_header Access-Control-Allow-Origin 'https://managerstag.matangame.com' always;
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Headers 'Authorization';
return 204;
}
#Access-Control-Allow-Methods 'POST, GET, OPTIONS';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#add_header Content-Security-Policy upgrade-insecure-requests;
proxy_pass http://managerstag.matangame.com/;
}
#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 8787;
server_name localhost;
location / {
add_header Content-Security-Policy upgrade-insecure-requests;
proxy_pass http://managerstag.matangame.com/;
}
}
# 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;
# }
#}
include servers/*;
}
tips:如果遇到http与https混用情况,需在nginx上server层,增加配置
add_header Content-Security-Policy "upgrade-insecure-requests;connect-src *";