实战nginx实现反向代理

1.下载tar包,或者去网站手动下载,wget http://nginx.org/download/nginx-1.11.6.tar.gz

2.解压到/usr/local/,  tar xzvf nginx-1.11.6.tar.gz -C /usr/local/

3.cd /usr/local/nginx-1.11.6切换到nginx安装目录

./configure进行初始化配置,出现报错./configure: error: C compiler cc is not found

解决:yum -y install gcc gcc-c++ autoconf automake make

继续报错:./configure: error: the HTTP rewrite module requires the PCRE library.说明该机器没有安装PCRE,而nginx需要依赖PCRE

解决:yum -y install pcre-devel

继续报错:./configure: error: the HTTP gzip module requires the zlib library.说还需要zlib library

解决:yum install -y zlib-devel,然后./configure初始化配置终于好了(但是提示没用openssl,yum -y install openssl* ,用来支持https)

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

4.make install编译
编译完成后会发生成了/usr/local/nginx/目录

5.启动nginx

cd /usr/local/nginx/sbin

./nginx

一般vps都会做了防火墙设置,需要手动添加规则开放80端口,并且iptables添加规则的顺序要注意,否则无效

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

service iptables save

service iptables restart

6.验证

验证端口是否能telnet

页面打开ip:80看能否跳转nginx页面

或者在服务器用curl抓取

curl http://localhost 出现以下内容则表示启动成功

[root@wts sbin]# curl http://localhost



Welcome to nginx!



Welcome to nginx!


If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to
nginx.org.

Commercial support is available at
nginx.com.

Thank you for using nginx.



 

7.nginx.conf配置

    server {
        listen       80;
    listen 443 ssl;
        server_name  www.abc.com;//填写你的域名
        #以下为 ssl 证书存放位置,证书在各大主机商都可以下载,使用https会比http流畅很多
        ssl_certificate /usr/local/nginx/cert/182.pem;
    ssl_certificate_key /usr/local/nginx/cert/182.key;
    #以下为强制跳转 https
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 497  https://$host$request_uri;
    location / {
            #需要代理的地址,这里为 谷歌
            proxy_pass https://www.google.com;
        proxy_connect_timeout 120;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        send_timeout 600; 

        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 

 

8.最后,nginx常用命令

cd /usr/local/nginx/sbin

  启动:./nginx

  停止:./nginx -s stop

  重启:./nginx -s reload

  执行./nginx -h 可以看到命令的帮助信息

FAQ:1.-bash: nginx: command not found

解决:配置环境变量  

参考export NGINX_HOME=/usr/local/nginx/sbin
       PATH=$PATH:$HOME/bin:$NGINX_HOME

        2.代理https时报错:nginx: [emerg] https protocol requires SSL support in /usr/local/nginx/conf/nginx.conf:51

解决:/usr/local/nginx/sbin/nginx -V查看配置

然后重新加载openssl模块./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

最后在源码包把nginx拷贝到/usr/local/nginx/sbin下:

cd /usr/local/nginx-1.11.6

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

cp ./objs/nginx /usr/local/nginx/sbin/   然后启动

                                                                                   

你可能感兴趣的:(实战nginx实现反向代理)