nginx安装配置
brew searchnginx
brewinstall nginx
很慢,要等很久
输出:The default port has been set in/usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
echo 'exportPATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile
启动:nignx
修改配置文件为自己需要的。
#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 {
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;
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 80;
server_name http://test.111.com;
location ~ \.(js|html)$ {
proxy_pass http://127.0.0.1:9306;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.163.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
location ~ \.(css)$ {
proxy_pass http://127.0.0.1:9306;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.111.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
location ~ /images/ {
proxy_pass http://127.0.0.1:9306;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.111.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
location ~ /style/ {
proxy_pass http://127.0.0.1:9306;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.163.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
location ~ /font/ {
proxy_pass http://127.0.0.1:9306;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.111.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
location ~ \_hmr$ {
proxy_pass http://127.0.0.1:9306;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.111.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
location / {
proxy_pass http://zc.dev.you.111.com;
proxy_read_timeout 7200;
proxy_connect_timeout 15;
proxy_set_header Host zc.dev.your.111.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 2000m;
break;
}
}
}
配置Host:127.0.0.1 test.111.com
启动本地项目,启动nginx。cd/usr/local/etc/nginx sudo nginx
/usr/local/etc/nginx-s reload # 重新载入配置文件
/usr/local/etc/nginx-s reopen # 重启 Nginx
/usr/local/etc/nginx-s stop # 停止 Nginxls
用转发功能,把前端的请求一部分转发到本地,ajax请求转发到服务器。
在浏览器输入http://test.dev.your.111.com/login 就是请求的服务器数据。
输入http://127.0.0.1:9306/login 就请求的是本地mock的数据。
原理:
Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,就可以按需求实现。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。
启动报错: