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

一,先看一下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 joe.mafei.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_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.mafei.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_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        joe.mafei.com
127.0.0.1        back.mafei.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  	joe.mafei.com
	127.0.0.1 	back.mafei.com

OK了!

nginx跨域配置如下:

location / {
			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";
			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,*';
    }

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