5,Centos7编译安装nginx并设置反向代理
一、编译部署Nginx 1.12
安装配置:
[root@localhost ~]# groupadd nginx [root@localhost ~]# useradd -s /sbin/nologin -g nginx -M nginx [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld #关掉selinux #服务器文件描述符等
[root@localhost ~]# yum install gcc gcc-c++ glibc automake pcre zlip zlib-devel openssl-devel pcre-devel wget lrzsz [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz [root@localhost src]# tar -zxvf nginx-1.12.0.tar.gz [root@localhost src]# cd nginx-1.12.0 [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre [root@localhost nginx-1.12.0]# make [root@localhost nginx-1.12.0]# make install
淘宝专用的Nginx 链接http://tengine.taobao.org/
备注:
--prefix:Nginx安装目录
--user:Nginx用户
--group:Nginx用户所属组
--with-http_ssl_module:提供https支持
--with-http_flv_module:搭建flv视频服务器使用的
--with-http_stub_status_module:开启Stub Status模块,该模块会产生一个服务器状态和信息页
--with-http_gzip_static_module:开启Gzip静态模块,该模块用于发送预压缩文件
--with-pcre:perl执行文件路径
配置服务:
[root@localhost nginx-1.12.0]# vi /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillMode=process KillSignal=SIGQUIT TimeoutStopSec=5 PrivateTmp=true [Install] WantedBy=multi-user.target
验证:
[root@localhost nginx-1.12.0]# chmod a+x /usr/lib/systemd/system/nginx.service [root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost nginx-1.12.0]# systemctl start nginx [root@localhost nginx-1.12.0]# ps -ef | grep nginx root 24412 1 0 16:31 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 24413 24412 0 16:31 ? 00:00:00 nginx: worker process root 24415 10541 0 16:31 pts/0 00:00:00 grep --color=auto nginx
访问:
二、nginx配置反向代理
设置nginx配置文件,并include proxy.conf文件:
[root@localhost conf]# cat nginx.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; proxy_pass http://127.0.0.1:7777; include proxy.conf; index index.php index.html index.htm; } #以下三行好像不用加 location ~ .*\.(js|css|jpg|png)$ { proxy_pass http://127.0.0.1:7777; } ****************
备注:如果只加proxy_pass一行信息,可能会造成页面能访问,但是css等样式文件加载不出来,只能显示文字信息。
proxy.conf配置文件:
[root@localhost conf]# cat proxy.conf # proxy.conf client_body_buffer_size 256k; client_body_temp_path client_body 1 2; client_max_body_size 50m; proxy_buffers 4 32k; proxy_buffer_size 4k; proxy_busy_buffers_size 64k; proxy_connect_timeout 30; proxy_read_timeout 60; proxy_redirect off; proxy_send_timeout 30; proxy_set_header Host $host; proxy_set_header X-Server-Addr $server_addr; proxy_set_header X-Remote-Addr $remote_addr; proxy_set_header X-Remote-Port $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Server-Type nginx; proxy_temp_file_write_size 64k; proxy_temp_path proxy_temp 1 2; proxy_max_temp_file_size 128m; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
重启nginx服务:
[root@localhost conf]# systemctl stop nginx
[root@localhost conf]# systemctl start nginx