centos里安装nginx1.16.1 & nginx反向代理 端口映射

各种依赖

#gcc安装,nginx源码编译需要

yum install gcc-c++

#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式

yum install -y pcre pcre-devel

#zlib安装,nginx 使用zlib对http包的内容进行gzip

yum install -y zlib zlib-devel

#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)

yum install -y openssl openssl-devel

1、下载

 wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
2、解压缩

tar -zxvf nginx-1.16.1.tar.gz

3、解压后进入目录

cd nginx-1.16.1

4、#使用默认配置

./configure

5、#编译安装

make

make install

6、#查找安装路径,默认都是这个路径

[root@VM_0_12_centos ~]# whereis nginx

nginx: /usr/local/nginx

7、#启动、停止nginx

cd /usr/local/nginx/sbin/

./nginx #启动

./nginx -s stop #停止,直接查找nginx进程id再使用kill命令强制杀掉进程

./nginx -s quit #退出停止,等待nginx进程处理完任务再进行停止

./nginx -s reload #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效

8、#重启nginx,建议先停止,再启动

./nginx -s stop

./nginx

9、#查看nginx进程,如下返回,即为成功

[root@VM_0_12_centos ~]# ps aux|grep nginx

root 5984 0.0 0.0 112708 976 pts/1 R+ 14:41 0:00 grep --color=auto nginx

root 18198 0.0 0.0 20552 612 ? Ss 11:28 0:00 nginx: master process ./nginx

nobody 18199 0.0 0.0 23088 1632 ? S 11:28 0:00 nginx: worker process

开机自启动

#在rc.local增加启动代码即可

vi /etc/rc.local

#增加一行 /usr/local/nginx/sbin/nginx,增加后保存

#设置执行权限

cd /etc

chmod 755 rc.local

浏览器输入服务器ip即可看到nginx欢迎界面

配置域名映射

#进入nginx配置文件目录,找到nginx的配置文件nginx.conf

cd /usr/local/nginx/conf/

#直接修改

vi nginx.conf

#listen为监听的端口

listen 80;

#server_name为域名

server_name www.test.com;

#location是访问地址的设置,locahost也可以用服务器ip代替

location / {

proxy_pass http://localhost:8080;

}

如图,只需要修改server_name和location里面的内容即可

#修改完成后,重新加载配置文件

cd /usr/local/nginx/sbin/

./nginx -s reload

进入域名控制台,添加或者修改解析地址,如果原来配置了解析,新解析需要一定时间才能生效

原文链接,https://my.oschina.net/yueshengwujie/blog/3099219

自己编写的如下


    server {
        listen       80;
        server_name  *.*.cn;

        #access_log  logs/host.access.log  main;

         location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:7000;
        }
    }

   server {
        listen       80;
        server_name  *.*.cn;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:9000;
        }

    }

猜你喜欢

转载自blog.csdn.net/zb0567/article/details/105543175
今日推荐