一、Windows下部署
官网:
https://nginx.org
Nginx大致有三个版本:
1、Mainline version:Main是Nginx的主力在做的版本,可以说是开发版
2、Stable version:最稳定版,生产环境上建议的版本
3、Legacy versions:遗留的老版本稳定版
下载安装包:
在安装包里面启动nginx.exe
当在浏览器里面输入localhost的时候出现
时说明默认端口被占用,需要解除占用在重新启动
简单部署:
方法一:直接在安装包里的html文件里部署项目即可(这也需要修改html文件夹里面的文件结构)
方法二:需要修改在安装包里conf文件夹里面的nginx.conf文件,不过需要在修改之后重新启动
location / {
# root html; // 原网页文件夹
root dist; // 新网页文件夹
index index.html index.htm; // 入口
}
error_page 500 502 503 504 /50x.html; // 错误时的网页及其位置
location = /50x.html {
root html;
}
二、Nginx解决前端跨域的相关部署
以下来于HelloKang
来源:https://www.jianshu.com/p/d9de7a5c4673
Nginx的负载均匀 可以将请求转发均匀转发至多台服务器上,通过部署多台相同服务的服务器,可以减轻服务器的压力。
接下来对nginx/conf下的nginx.conf文件做如下配置:
#user nobody;
# 进程数可根据cpu数量调整
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;
# 连接超时时间,服务器会在这个时间过后关闭连接(10)。
keepalive_timeout 65;
# gizp压缩
gzip on;
# srever模块配置是http模块中的一个子模块,用来定义一个虚拟访问主机
server {
# 监听80端口
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 根路径指到index.html
location / {
root html;
index index.html index.htm;
}
# /api会被分发到myserver
location /api {
# 负载均衡名(用于进行负载均衡的配置)
proxy_pass http://myserver;
# 设置用户真实ip否则获取到的都是nginx服务器的ip
proxy_set_header X-real-ip $remote_addr;
proxy_set_header Host $http_host;
}
#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;
#}
}
#负载均衡的配置
upstream myserver {
# server后是两个服务的地址,以及参数设置, weight(权重), max_fails(最大失败次数), fail_timeout(请求失败暂停时间后重新请求).
server 192.168.0.103:8080 weight=1 max_fails=2 fail_timeout=20s;
server 192.168.0.103:8081 weight=1 max_fails=2 fail_timeout=20s;
# ip_hash;
# ip_hash将某个ip的访问都定向到同一台服务器上,可以解决session问题
# 但ip_hash是有弊端的(具体百度)
# 为了解决ip_hash的一些问题,可以使用upstream_hash这个第三方模块
}
# 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;
# }
#}
}
nginx -s reload 重新载入nginx配置.
如果想解决跨域问题
可以参看这里幸福丶如此
来源:https://blog.csdn.net/m0_37886429/article/details/83618506
或者Developer
来源:https://segmentfault.com/a/1190000012550346
三、linnx的部署Nginx
linux centos一般用于服务器(稳定),linux ubuntu一般用于学习(可用图形用户界面操作)
linux centos上面部署Nginx
可使用WinSCP来在Windows上操作远程的linux系统(使用ssh)
/*在linux系统里面部署*/
yum install nginx
/*开启nginx服务*/
systemctl start nginx.service
/*跟随系统启动*/
systemctl enable nginx.service
/*使用vim指令来修改nginx.conf文件*/
vim nginx.conf
/*敲 i ,代表insert*/
/*敲 esc ,代表退出编辑*/
/*敲 :wq ,代表保存并且退出*/
也可以使用WinSCP找到文件,并右键编辑(或者先把文件拖到本地修改,再覆盖到远程的linux上面)
/*修改之后重启*/
nginx -s reload
本文只用于个人学习与记录