您的位置:首页 > 运维架构 > 反向代理

nginx反向代理

2019-05-23 23:42 731 查看

Nginx反向代理部署
以下是所需安装包
链接:https://pan.baidu.com/s/15rZS36XAes-0OidVudJBWA
提取码:9kk9
复制这段内容后打开百度网盘手机App,操作更方便哦
一、 安装nginx
声明我的是centos7.5系统最小化安装
先行安装nginx的依赖包
[root@centos-1 ~]# mount /dev/cdrom /media

  • make libtool zlib zlib-devel pcre pcre-devel openssl openssl-develmount: /dev/sr0 写保护,将以只读方式挂载
    [root@centos-1 ~]# cd /etc/yum.repos.d/
    [root@centos-1 yum.repos.d]# mkdir bak
    [root@centos-1 yum.repos.d]# mv * bak/
    mv: 无法将目录"bak" 移动至自身的子目录"bak/bak" 下
    [root@centos-1 yum.repos.d]# echo ‘[media]’ >> media.repo
    [root@centos-1 yum.repos.d]# echo ‘name=media’ >> media.repo
    [root@centos-1 yum.repos.d]# echo ‘baseurl=file:///media’ >> media.repo
    [root@centos-1 yum.repos.d]# echo ‘enable=1’ >> media.repo
    [root@centos-1 yum.repos.d]# echo ‘gpgcheck=0’ >> media.repo
    [root@centos-1 yum.repos.d]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
    创建用户和组并安装
    [root@centos-1 ~]# groupadd www
    [root@centos-1 ~]# useradd -g www -s /sbin/nologin -M www
    [root@centos-1 ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
    –2019-05-23 23:10:15-- http://nginx.org/download/nginx-1.16.0.tar.gz
    正在解析主机 nginx.org (nginx.org)… 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
    正在连接 nginx.org (nginx.org)|95.211.80.227|:80… 已连接。
    已发出 HTTP 请求,正在等待回应… 200 OK
    长度:1032345 (1008K) [application/octet-stream]
    正在保存至: “nginx-1.16.0.tar.gz”

100%[====================================================================================================>] 1,032,345 53.2KB/s 用时 24s

2019-05-23 23:10:39 (42.0 KB/s) - 已保存 “nginx-1.16.0.tar.gz” [1032345/1032345])

[root@centos-1 ~]# tar zxf ngx_cache_purge-2.3.tar.gz
[root@centos-1 ~]# tar zxf master.tar.gz
[root@centos-1 ~]# tar zxf nginx-1.16.0.tar.gz
[root@centos-1 ~]# cd nginx-1.16.0
[root@centos-1 nginx-1.16.0]# ./configure --prefix=/usr/local/nginx1.10 --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=…/ngx_cache_purge-2.3 --with-http_flv_module --add-module=…/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 && make -j 2 && make install
[root@centos-1 nginx-1.16.0]# cp /usr/local/nginx1.10/sbin/* /usr/local/sbin/
[root@centos-1 nginx-1.16.0]# chmod +x //usr/local/sbin/*
[root@centos-1 nginx-1.16.0]# nginx
nginx: [emerg] mkdir() “/var/tmp/nginx/client” failed (2: No such file or directory)
[root@centos-1 nginx-1.16.0]# mkdir /var/tmp/nginx/client
mkdir: 无法创建目录"/var/tmp/nginx/client": 没有那个文件或目录
[root@centos-1 nginx-1.16.0]# mkdir /var/tmp/nginx/client -p
[root@centos-1 nginx-1.16.0]# chown -R www.www /var/tmp/nginx/
[root@centos-1 nginx-1.16.0]# nginx
[root@centos-1 nginx-1.16.0]# [root@centos-1 nginx-1.16.0]# firewall-cmd --permanent --add-port=80/tcp
success
[root@centos-1 nginx-1.16.0]# firewall-cmd --reload
success
[root@centos-1 nginx-1.16.0]# netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4398/nginx: master
二、 修改配置文件

[root@centos-1 ~]# cat /usr/local/nginx1.10/conf/nginx.conf

#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;
upstream backend {
sticky;
server 192.168.1.102:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.1.103:80 weight=1 max_fails=2 fail_timeout=10s;
}
#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       80;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   html;
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;
}

# 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;
#    }
#}

}

[root@centos-1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx1.10/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.10/conf/nginx.conf test is successful
[root@centos-1 ~]# nginx -s reload
Nginx反向代理到这里配置ok了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: