linux下nginx安装、操作、配置、问题及解决<一>

-----------------------------------------------安装--------------------------------------------------------
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
nginx下载地址:https://nginx.org/download/

--解压
tar -zxvf nginx-1.9.9.tar.gz  或者  tar -zxvf nginx-1.9.9.tar.gz -C /home/nginx
--进入nginx目录
cd nginx-1.9.9
--配置
./configure --prefix=/usr/local/nginx
--make
make
make install
--检测是否安装成功到/usr/local/nginx目录下
./sbin/nginx -t

---下面根据情况进行配置(可不用配置)
--开启80端口:
firewall-cmd --add-port=80/tcp --permanent
#重启防火墙
systemctl restart firewalld

--配置nginx开机自启动
vim /etc/rc.d/rc.local
文件中添加  /usr/local/nginx/sbin/nginx


----------------------------------------------nginx常用操作---------------------------------------------------
到nginx的安装目录下
--快速停止或关闭Nginx:./nginx -s stop
--正常停止或关闭Nginx:./nginx -s quit
--配置文件修改重装载命令:nginx -s reload


-------------------------------删除nginx相关的所有配置(用于重装nginx)-----------------------------------------
--停止nginx
  service nginx stop
--删除nginx自动启动
  chkconfig nginx off
--从源头删除Nginx (whereis nginx)
        rm -rf /usr/sbin/nginx
        rm -rf /etc/nginx
        rm -rf /etc/init.d/nginx
--再使用yum清理
  yum remove nginx

---------------------------------------conf相关的配置介绍--------------------------------------------------------
目录在nginx安装目录的conf/nginx.conf下
---------- 重点配置----------------
    #test1 采用这种方式有助于后面增加负载,hash或其它负载方式
    upstream test1sss {
      server localhost:8090; # 对应当前本机tomcat1(测试)
    }
    #test2
    upstream test2sss {
      server localhost:8091; # 对应当前本机tomcat2(测试)
    }

server {
    # 监听80端口
    listen       80;
    server_name  www.pytest1.xxx.com pytest1.xxx.com;
    access_log on;
    #charset koi8-r;
     charset utf-8;
    # 不加有时是不会显示目录的
    autoindex on;
    #access_log  logs/host.access.log  main;

    location /test1 {
            proxy_pass http://test1sss/;
            proxy_set_header  Host  $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    # 
    location /test2 {
            proxy_pass http://test2sss/;
            proxy_set_header  Host  $host;
            proxy_connect_timeout 5s;
    }

    location / {
       root html;
       index index.html index.htm;
    }
    # 此处访问地址为/home/dist1
    location /dist1 {
       alias /home/dist1/;
       index index.html index.htm;
    }
    # 此处访问地址为/home/dist2/dist2
    location /dist2 {
       root /home/dist2/;
       index index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

 ----------------其它示例配置--------------
  upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
  }
   # 缓存资源8小时
  location ~* \.(gif|jpg|jpeg|png|css|js) {
           expires 8h;
   }

   location  /change/ {
           proxy_pass http://localhost:9090/;
           proxy_buffering on;
           proxy_buffer_size 32k;
                   proxy_buffers 4 128k;
                   proxy_busy_buffers_size 256k;
                   proxy_max_temp_file_size 256k;
                   proxy_connect_timeout 60s;
                   proxy_send_timeout 60s;
                   proxy_read_timeout 60s;
                   proxy_set_header Host $http_host;
   }


  
------------------------------logs下面的nginx pid被删除的解决办法-------------------------------------------
  查询nginx进程号并写入logs下的pid文件
  ps -ef | grep nginx
-- 在logs下创建文件
  touch nginx.pid
-- 写入文件
 echo "34222" > nginx.pid
-- 重启服务:
 ./nginx -s reload

-------------------------------------前端代理相关配置--------------------------------------------------------
 location / {
        root /home/testdir/build;
        index index.html index.htm;
        try_files $uri /index.html; #加上这句解决react 当前路由下 刷新 报404的问题
        if (!-e $request_filename) {
          rewrite ^.+testdir/(.*) /index.html last;
          break; 
        }
     }
      
      location /api {
        rewrite ^.+api/?(.*)$ /$1 break;
        proxy_pass http://localhost:8095;
    }

在docker里面安装nginx配置起来个人觉得挺麻烦,所以一般情况下选择提前下载好安装包直接在linux下安装的方式,文章仅供参考,具体安装方式也可根据具体情况而定。
nginx前端实践配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值