nginx学习---本地运行nginx启动react项目
#笔者前端入门水平,最近打算学习下webpack,所以就在本地安装了nginx用来测试webpack效果。
安装
这个安装教程可就太多了,不管是linux还是windows的网上都是一大堆,自行百度或者用我下面这一个链接也行:https://blog.csdn.net/qq_33454884/article/details/89212702
安装完后你会发现在目录下有这么几个文件。
当然最后两个是我自己添加的重启和停止的命令,代码如下:
restart.bat
@echo off
%~d0
cd %~dp0
nginx -s reload
stop.bat
@echo off
%~d0
cd %~dp0
nginx -s quit
当你在浏览器地址栏输入localhost跳出这个页面就代表安装成功并运行起来了
说傻瓜安装一点都不为过吧!
nginx.conf配置
简单的代理
网上关于这个配置说明的帖子有很多我也是参考他们的,就不班门弄斧了,万一等下还说错了就不好了。
我将我在github上下的一哥react项目build后生成的dist文件放到了nginx安装目录下的html文件夹下
#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;
server {
listen 3333;
server_name http://127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/dist;
index index.html index.htm;
}
#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;
}
location ^~ /user/ {
proxy_pass http://xx.xxx.xxx.xx:xxxxx/user-service/;
index index.html index.htm index.jsp;
}
location ^~ /smz/ {
proxy_pass http://xx.xxx.xxx.xx:xxxxx/smz-service/;
index index.html index.htm index.jsp;
}
# 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 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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;
# }
#}
}
主要的修改也就在server和location里。server是监听本地的端口号;location是映射本地端口号和本地项目配置。简单说就是3333端口监听本地nginx安装目录下的html/dist里的index.html文件。最后保存,重启nginx。(每次修改配置都需要重启一次nginx)
最终效果如下:
端口号可以自行修改
接口代理
上面的是使用mock生成假数据的项目,使用真实接口的配置也是一样的,代码在上面也有,如下:
location ^~ /user/ {
proxy_pass http://..服务器地址../user-service/;
index index.html index.htm index.jsp;
}
location ^~ /smz/ {
proxy_pass http://xx.xxx.xxx.xx:xxxxx/smz-service/;
index index.html index.htm index.jsp;
}
其中的*/user/和/smz/*是react多代理配置(setupProxy.js)文件中得代理名称,这样,项目就能在本地的nginx上跑起来了,反向代理配置成功。一整个流程下来觉得真的是傻瓜配置。
接下来就是学习下webpack打包配置了,笔者已经不能忍受每次提交代码都要去修改测试服务器和开发服务器地址,期待我的下一篇帖子吧!