CentOS7安装Nginx及配置域名映射反向代理

1.安装gcc

yum -y install gcc

2.pcre、pcre-devel安装

yum install -y pcre pcre-devel

3.zlib安装

yum install -y zlib zlib-devel

4.安装openssl

yum install -y openssl openssl-devel

5.下载nginx安装包

wget http://nginx.org/download/nginx-1.9.9.tar.gz  

6.解压

tar -zxvf  nginx-1.9.9.tar.gz

7.切换到cd /usr/local/nginx/下面
执行三个命令

./configure
 
make
 
make install

8.配置nginx的配置文件nginx.conf文件

cd /usr/local/nginx/conf
vim nginx.conf

nginx.conf的具体配置如下
主要参数说明:
server_name:配置访问的域名
proxy_pass:配置项目的访问地址


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

    #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; #首先是nginx的监听端口默认为80
        server_name  www.hqzou.top; #这里是你需要访问的域名地址
		#add_header 'Access-Control-Allow-Origin' '*';#这里是http 域名跨域,报错时候添加的请求头,这样写所有请求都会进来,会很不安全。
        #charset koi8-r;
        #access_log  logs/host.access.log  main;#这里是 日志文件的生成路径
        
		#详细介绍location
        location / {
        	#是监听的端口默认访问的地址,这里如果没有做tomcat的转发则会进入nginx的html目录下的index.html
            root   html;
            
            #这里是编写监听到的请求所转发的端口号,即tomcat端口
			proxy_pass http://localhost:8081;
            #Proxy Settings;
            #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;
			
			#设置nginx 的默认显示页
            index  index.html index.htm;

			#设置http请求的请求头,使其在跨域访问上不会被浏览器阻止。ps:这里设置我发现没有用,后来还是在ajax过滤器中添加的 请求头,如果大家有知道这里怎么修改的,请留言大家一起学习。
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
			add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

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

}

你可能感兴趣的:(SpringCloud,nginx)