Detailed explanation of Nginx multi-site configuration examples

This article mainly introduces relevant information about Nginx multi-site configuration examples. Friends in need can refer to the following

Detailed explanation of Nginx multi-site configuration examples

On a VPS, we sometimes need to run several virtualenv at the same time. For example, virtualenv app1 runs a Django application, while virtualenv app2 runs Tornado. So how to configure Nginx so that it supports the operation of these two virtualenvs at the same time?

The first is the main configuration of Nginx, located in etc/nginx/ngnix.conf. Just leave it as the default:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid    /var/run/nginx.pid;

events {

  worker_connections 1024;

}

http {

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

  sendfile    on;

  #tcp_nopush   on;

  keepalive_timeout 65;

  #gzip on;

  server {

    listen    80;

    server_name 112.124.7.216;

    #server_name localhost;

    #if ($host != 'www.nowamagic.net' ) {

    #  rewrite ^/(.*)$ http://www.nowamagic.net/$1 permanent;

    #}

    access_log /home/nowamagic/logs/access.log;

    error_log /home/nowamagic/logs/error.log;

    #root     /root/nowamagic_venv/nowamagic_pj;

    location / {

      uwsgi_pass 127.0.0.1:8077;

      #include uwsgi_params;

      include /etc/nginx/uwsgi_params;

      #uwsgi_pass 127.0.0.1:8077;

      #uwsgi_param UWSGI_SCRIPT index;

      #uwsgi_param UWSGI_PYHOME $document_root;

      #uwsgi_param UWSGI_CHDIR $document_root;

    }

    location ~ \.php$ {

      #root     html;

      root      /var/www/html;

      fastcgi_pass  127.0.0.1:9000;

      fastcgi_index index.php;

      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

      include    fastcgi_params;

    }

    access_log off;

  }

  include /etc/nginx/conf.d/*.conf;

}

Notice this sentence, include /etc/nginx/conf.d/*.conf; It will load all configuration files in the conf.d folder. Then the next thing is simple. We design two .conf files, one is the django configuration and the other is the tornado configuration.

1. app1_django.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

server {

  listen    80;

  server_name 112.124.7.216;

  #server_name localhost;

  #if ($host != 'www.imofa.net' ) {

  #  rewrite ^/(.*)$ http://www.imofa.net/$1 permanent;

  #}

  access_log /home/nowamagic/logs/access.log;

  error_log /home/nowamagic/logs/error.log;

  #root     /root/nowamagic_venv/nowamagic_pj;

  location / {

    uwsgi_pass 127.0.0.1:8077;

    #include uwsgi_params;

    include /etc/nginx/uwsgi_params;

    #uwsgi_pass 127.0.0.1:8077;

    #uwsgi_param UWSGI_SCRIPT index;

    #uwsgi_param UWSGI_PYHOME $document_root;

    #uwsgi_param UWSGI_CHDIR $document_root;

  }

  location ~ \.php$ {

    #root     html;

    root      /var/www/html;

    fastcgi_pass  127.0.0.1:9000;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    include    fastcgi_params;

  }

  access_log off;

}

The following is the configuration of tornado:

2. app2_tornado.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

upstream tornado {

  server 127.0.0.1:8888;

}

  

server {

  listen  80;

  root /root/nmapp2_venv;

  index index.py index.html;

  

  server_name server;

  

  location / {

    #if (!-e $request_filename) {

    #  rewrite ^/(.*)$ /index.py/$1 last;

    #}

  }

  

  location ~ /index\.py {

    proxy_pass_header Server;

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Scheme $scheme;

    proxy_pass http://tornado;

  }

}

Restart Nginx:

1

service nginx restart

OK, the apps in both virtual environments can be accessed.

Thanks for reading, I hope it helps everyone, thank you everyone!

Source: Weidian Reading    https://www.weidianyuedu.com

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131764323
Recommended