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 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跨域配置如下:

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,*';
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章