nginx同一个端口配置多个网站(以及nginx跨域配置)

14 篇文章 0 订阅
6 篇文章 0 订阅

一,先看一下nginx.conf文件,如果有下面的一行
include /etc/nginx/sites-enabled/*;
表示配置文件加载sites-enabled下的文件。
二,修改/etc/nginx/sites-enabled下的default文件
内容如下

server {
        listen 80;
        root /usr/share/nginx/html/front-api/public;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;
        server_name front.web.com;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                //上面这一行,是解析动态网址,如果是需要访问静态网址的话,就改成try_files $uri $uri/ =404;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
         # fastcgi_pass   unix:/run/php/php7.2-fpm.sock;//如果使用上面的9000端口地址访问出错502的话,就换成这个(注意该sock文件的权限问题)
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #   fastcgi_param  PATH_INFO  $fastcgi_path_info;
        #   fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

server {
        listen 80;
        root /usr/share/nginx/html/back-api/public;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;
        server_name back.web.com;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                //上面这一行,是解析动态网址,如果是需要访问静态网址的话,就改成try_files $uri $uri/ =404;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
        #  fastcgi_pass   unix:/run/php/php7.2-fpm.sock;//如果使用上面的9000端口地址访问出错502的话,就换成这个(注意该sock文件的权限问题)
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #   fastcgi_param  PATH_INFO  $fastcgi_path_info;
        #   fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

三,
sudo vi /etc/hosts
添加如下内容

127.0.0.1        front.web.com;
127.0.0.1        back.web.com;

四,重启nginx sudo /etc/init.d/nginx restart

五,本地,打开C:\Windows\System32\drivers\etc\hosts
如果无法修改,需要右键,属性,修改其文件权限。
修改添加内容如下

	127.0.0.1   128.1.2.41
	127.0.0.1  	front.web.com;
	127.0.0.1 	back.web.com;

OK了!

nginx跨域配置如下:

server {

listen       80;
server_name  192.168.100.76;
index index.php index.html;
root "D:/worker/sdgf_huodong/public";

location / {
        try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php(.*)$ {
	if ($request_method = OPTIONS ) {
	add_header Access-Control-Allow-Origin "*";
	add_header Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, DELETE";
	add_header Access-Control-Max-Age "3600";
	add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization, token";
	add_header Access-Control-Allow-Credentials "true";
	add_header Content-Length 0;
	add_header Content-Type text/plain;
	return 200;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, token*';
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值