Nginx多端口转发文件配置

Nginx多端口转发文件配置

服务器使用非80端口访问需要加端口号还是很难受的,使用域名解析来绑定也比较不划算,例如阿里云免费版的匿名转发url服务只有两个,如果使用Nginx进行转发,还是很爽的。
只需要将所有的二级域名解析到服务器的IP(阿里云域名解析到IP是没多大限制的),用Nginx进行识别访问的域名,进行转发,体验不输域名解析服务。这里给出目前在用的配置文件示例

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    #网页使用nginx上传文件时的最大文件限制
    client_max_body_size 20m;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
	
	#一条转发记录,一个server
    server {
        listen       80;    #监听的端口
        listen       [::]:80;
		#服务名写入你的域名,例如二级域名,只需要在云解析服务把所有二级域
		#名解析到服务器的IP,在这里会进行服务名(访问的域名)匹配
		#例如以域名 t.test.com访问,就会匹配到以下服务名
        server_name  t.test.com;
		
		#你的需要转发的跟目录,也就是你的显示的页面的目录,算是
		#index.html的文件夹,例如存在/var/www/html/test/index.html
		#则根目录可以写成/var/www/html/test
        root         /var/www/html/test;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        #进入文件夹检索的转发的界面文件,左边的优先
                index index.php index.html index.htm;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
   }


server {
        listen       80;
        listen       [::]:80;
        #例如t2.test.com
        server_name  t2.test.com;
        #需要转发/var/www/html/t2/index.html
        root         /var/www/html/t2;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                index index.php index.html index.htm;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


#转发php网页,需要安装并开启php的组件
server {
        listen       80;
        listen       [::]:80;
        #例如php.test.com
        server_name  php.test.com;
        #如果需要转发/var/www/html/php/index.php
        root         /var/www/html/php;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                index index.php index.html index.htm;
        }

        location ~ .php$ {
            root /usr/share/nginx/html/wordpress;    #将/usr/share/nginx/html替换为您的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录
            fastcgi_pass 127.0.0.1:9000;   #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;   #Nginx调用fastcgi接口处理PHP请求
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

 
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值