浅谈前后端分离中nginx的应用

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
		
		###############ldweb
		location / {
			proxy_pass              http://127.0.0.1:9000;
			proxy_redirect          off;
            proxy_set_header        Some-Thing $http_x_custom_header;
            proxy_set_header        Accept-Encoding 'gzip';
            proxy_set_header        Host $host:$server_port;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    2m;
            client_body_buffer_size 500k;
            proxy_buffer_size       128k;
            proxy_buffers           8 128k;
            proxy_busy_buffers_size 256k;
            proxy_temp_file_write_size 1024k;
		}	 
        ###############ldweb
		
		###############ldapi
		location /ldapi/ {
			proxy_pass              http://127.0.0.1:8001/ldapi/;
			proxy_redirect          off;
            proxy_set_header        Some-Thing $http_x_custom_header;
            proxy_set_header        Accept-Encoding 'gzip';
            proxy_set_header        Host $host:$server_port;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    2m;
            client_body_buffer_size 500k;
            proxy_buffer_size       128k;
            proxy_buffers           8 128k;
            proxy_busy_buffers_size 256k;
            proxy_temp_file_write_size 1024k;
		}	 
        ###############ldapi
		
		
		#location ^~/proxy/html/{
		#rewrite ^/proxy/html/(.*)$ /$1 break;
		#proxy_pass http://localhost:8086;
		#}
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

nginx在这里是用于跨域请求的,我的前端我采用vuejs实现,是一个单独的项目,然后我后端也是一个单独的项目,他们分别拥有自己的访问端口号和过滤地址。nginx自己的端口号是80

后端访问地址ldapi:http://127.0.0.1:8081/ldapi/

前端访问地址ldweb:http://localhost:9000

上面两个location,一个是前端的,一个是后端的这样前后就可以成功访问后端的json数据。


用前端的访问地址,我们按F12,显示的请求地址就是后端的服务。上面显示红色的就是静态资源加载,一般我们就是把vue打包后生成的图片等资源放在服务器的webroot里面,红色那个就是配置服务器静态资源的路径

文章来源: 浅谈前后端分离中nginx的应用

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:浅谈前后端分离中nginx的应用