搭建Nginx服务器与简单使用

安装nginx

准备一个Linux云服务器

1.下载相关依赖(我的是centos系统)

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre-devel

2.将下载好的nginx 压缩包解压,然后进入nginx 目录下

tar -xvf ......  -C /usr/local

解压目录如下

按顺序执行

./configure
make && make install

再回退到上一个local下的目录=>多出一个nginx

修改conf 下面的nginx.conf配置

如果不是80就修改80,我们输入localhost默认端口是80,就像路由redirect跳转一样

再启动nginx

进入sbin目录下

启动 ./nginx

重启 ./nginx -s reload

关闭 ./nginx -s stop

测试 浏览器输入域名即可

负载均衡

upstream blog_real_servers {
         server 10.0.0.9:80  weight=5;
         server 10.0.0.10:80  weight=10;
         server 10.0.0.19:82  weight=15;
    }
    server {
       listen       80;
       server_name  blog.etiantian.org;
       location / {
        proxy_pass http://blog_real_servers;
       proxy_set_header host $host;
       }
    }
自己做的测试,比如我一个服务器,我80端口弄个负载均衡,映射到3000端口的应用,或者映射到8080端口的应用
upstream nuxttest{ #分配需要代理的服务
    #都不加weight就是轮询
    server localhost:8080 weight=2; #代理本地的vue服务
    server localhost:3000  weight=1; #代理本地的nuxt服务
}
server {
  listen 80;
  server_name jinruixiang.top; #你的域名

  location / {
    # 代理upstream模块(这里不能直接代理端口否则_nuxt文件目录无法找到)
   proxy_pass http://nuxttest; 
   index index.html index.htm;
   # try_files $uri $uri/ /index.html; #解决页面刷新404问题
  }
        # 这边是我配置的ftp资源服务器,搭建在我博客里
 location /files {
	alias  /home/user-file/files;
  }

}
# nuxt end

    server {
        listen       8080;
        server_name  birthday;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root    /usr/local/nginx/dist;
            index  index.html index.htm;
        }
	    location /files {
	        alias  /home/user-file/files;
        }


        location /api/ { 
        proxy_pass   http://115.29.209.198:9000; 
        rewrite  ^/api/(.*)$ /$1 break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

补充:多个域名配置在一台服务器上,可以通过域名访问不同的网页,推荐链接,我已经试过了有效,前提你购买了多个域名并进行备案与解析

部署在nginx上,有的页面刷新直接404

经验分享 程序员 微信小程序 职场和发展