【新手上路】Ubuntu安装配置nginx并设置端口转发

本人新手程序员,最近在写项目的有个需求,两个Springboot项目的端口号分别是8100和8888,都部署在同一个服务器上。访问时通过不同的URL的将请求转发到相应的Springboot项目里进行处理。这就需要用到Nginx来做一个反向代理,监听81端口,将81端口的请求转发,从而实现目的。


服务器环境为:Ubuntu 14.04.1 LTS

1. 为Ubuntu安装Nginx

安装Nginx的方法有很多种,我选择了比较偷懒的方法,利用apt-get install的方法

apt-get update
apt-get install nginx

2. 配置nginx

安装完成后我们可以查看nginx安装在了那里

whereis nginx


可以看出,Nginx的主目录被放在了/etc/nginx下,进入这个目录,发现有个名叫nginx.conf的文件。没错,这个就是nginx的配置文件,我们打开它,并加以修改,修改之后的配置文件是这样的

user www-data;
worker_processes 1; 
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
server {
        listen       81;
        server_name  127.0.0.1; 
        location /small {
        proxy_set_header Host $host:81;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8100/;
        }
        location /socket {
        proxy_set_header Host $host:81;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8888;
        }
    }

}

#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

有几个地方需要注意:

    worker_processes 1; 表示处理器核心数量,我这里是单核,所以是1

server {
        listen       81; #这里表示被监听的端口
        server_name  yoursite.com; #这里是服务器域名或者ip
        location /small {       #这里需要特别注意,/small表示路由路径,配置完成后访问yoursite:81/small会被转发到8100端口
        proxy_set_header Host $host:81;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8100/; #这里表示转发到8100端口
        }
        location /socket {    #/socket表示路由路径,配置完成后访问yoursite:81/socket会被转发到8888端口
        proxy_set_header Host $host:81;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8888/;
        }
    }

基本配置就结束了,保存退出后我们开启Nginx

service nginx reload
service nginx start

3. 开始测试

打开浏览器,输入定义好的网址

【新手上路】Ubuntu安装配置nginx并设置端口转发_第1张图片

注意看网址,是/small,说明已经路由成功。

接着我们测试通过8100端口访问该页面

【新手上路】Ubuntu安装配置nginx并设置端口转发_第2张图片

同样能进入该页面。大功告成!!!!

你可能感兴趣的:(【新手上路】Ubuntu安装配置nginx并设置端口转发)