linux服务器配置nginx反向代理

第一步下载安装nginx
1.下载安装nginx前,需要先下载相关的编译环境。
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
2.下载安装nginx
nginx下载地址:https://nginx.org/download/
下载“nginx-1.9.9.tar.gz”,移动到/usr/local/下。

		## 解压
		tar -zxvf nginx-1.9.9.tar.gz
		
		##进入nginx目录
		cd nginx-1.9.9
		
		linux安装软件采用源码安装灵活自由,适用于不同的平台,维护也十分方便。
		源码的安装一般由3个步骤组成:
		配置(configure)
		编译(make)
		安装(make install)

		## 配置 configure
		./configure --prefix=/usr/local/nginx
		
		# 编译 make
		make
		
		## 安装 make install
		make install

		测试是否安装成功
		# cd到刚才配置的安装目录/usr/local/nginx/
		./sbin/nginx -t
		
		错误信息:
			nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
			2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)
		原因分析:nginx/目录下没有logs文件夹
		解决方法:
			mkdir logs
			chmod 700 logs
		
		正常情况的信息输出:
			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
		
		启动nginx 
			cd /usr/local/nginx/sbin
			./nginx //启动nginx


在浏览器中输入服务器的ip地址,如:192.168.1.12
如果打不开链接。进行原因的排查:
	服务器的80端口可能是打不开的。
	因为我使用的linux系统版本是CentOS7,所以可以在服务器中执行如下命令来验证》》
	firewall-cmd --query-port=80/tcp
  
	显然80端口没有开启。
	下面我们开启80端口:
		firewall-cmd --add-port=80/tcp --permanent
		#重启防火墙
		systemctl restart firewalld
		 --permanent   #永久生效,没有此参数重启后失效

	如果nginx启动成功,将显示:

在这里插入图片描述

配置nginx开机自启动 //这里我还没配置 自启动文件如下 vim是编辑 具体配置我还不是很清楚 可以跳过这一步
	vim /etc/rc.d/rc.local

安装完 nginx后 就是配置nginx文件了
vim nginx.conf
编写配置文件
配置如下:

server {
        
        listen       80; 监听端口
        
        server_name  localhost; 监听地址
       
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /root/web/web-us/output; 对应项目的地址
            index  index.html index.htm; 首页面
        }

三、Nginx的配置
要运行起自己的网站我们还需要对Nginx做一些配置,在nginx文件夹的子文件夹conf下的nginx.config文件就是Nginx的配置文件,其文件内容如下:

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





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

}
#显然代表注释,以下是这些配置的一些说明

#使员工Nginx的用户名
#user  nobody;

#cpu数,一般设置成和服务器的cpu数一致
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程id
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {

   	#设置mime类型,类型由mime.type文件定义
    include       mime.types;
    default_type  application/octet-stream;
    
    #设定日志格式
    #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指令指定Nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设定为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime
    sendfile        on;
    #tcp_nopush     on;
    #设置超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #是否开启gzip压缩(网页速度优化非常有用,开启后通常可以达到70%的压缩率)
    #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;
        }
    
        #定义错误提示页面,你还可以在这里添加500,403等,以空格分开
        #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;
    #    }
    #}
}

看上面的这一串可能会眼花缭乱,其实重点很短:

server {
        
        #侦听端口    
        listen       80;
        
        #域名
        server_name  localhost;
        
        #编码设置
        #charset koi8-r;
        		
        #设定虚拟主机的访问日志
       	 =#access_log  logs/host.access.log  main;
       	 
        #默认请求
        location / {
            #默认网站的根目录
            root   html;
            #首页索引文件的名称
            index  index.html index.htm;
        }
    
        #定义错误提示页面,你还可以在这里添加500,403等,以空格分开
        #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;
        }
   }

再精简一点:

server {   
        #侦听端口    
        listen       80;
        
        #域名
        server_name  localhost:82;

        #默认请求
        location / {
            #node服务器启动的端口
            	proxy_pass http://127.0.0.0.1:82; 
      			root blog;
    }
    #这里我node服务器启动的端口是82端口 所以我这个配置的意思是 监听80端口(默认端口) 代理82端口的请求 也就是进入80端口会打开82端口的页面 
    很简洁的配置 效果就是打开我的网址 就能进入 我配置的82端口的网站了
    这就是最基本的代理服务器。
    还有一些骚操作 gzip提升性能的 就以后再研究吧~
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值