ubuntu环境下 nginx 怎么配置文件

简介: ubuntu环境下 nginx 怎么配置文件

@[toc]

nginx安装

首先是安装nginx,环境依然是ubuntu12.04(64位),通过下面命令:

sudo apt-get install nginx

nginx启动

安装好之后就是启动,目前我知道的在ubuntu下有两种启动方式:

sudo /etc/init.d/nginx start #通过init.d下的启动文件启动。
sudo service nginx start #通过ubuntu的服务管理器启动

nginx打开

在浏览器中输入http://localhost,看看是不是出现“Welcome to nginx!”的页面。如果没有的话,先继续往下看配置。

nginx配置

在我的系统中nginx的配置文件在/etc/nginx下。
打开nginx.conf文件,配置如下:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;

    keepalive_timeout     3600;
    client_header_timeout 3600;
    client_body_timeout   3600;
    proxy_connect_timeout 3600;
    proxy_send_timeout    3600;
    proxy_read_timeout    3600;

    types_hash_max_size 2048;

    client_max_body_size  36G;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # Mccts-start
    server {
        listen          2333;
        server_name     localhost;
        root            /home/mccts/start;
        index           index.html index.htm;

        location / {
            # 截取404的uri,传给 @router
            try_files $uri $uri/    @router;
        }

        location @router {
             # 接到截取的uri 并按一定规则重写uri和vue路由跳转
            rewrite ^.*$    /index.html last;
        }
    }

    # McctsControl
    server {
        listen        9527;
        server_name   localhost;
        autoindex     on;
        root          /home/mccts/mccts-control/web;
        index         index.html index.htm;

        location / {
            # 截取404的uri,传给 @route
            try_files $uri $uri/   @router;
        }

        location /control/ {
            proxy_pass          http://127.0.0.1:8080/;
            #proxy_pass          http://192.168.1.28:8080/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
          }

          location /analysis/ {
            proxy_pass          http://127.0.0.1:8081/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
          }

      location /Rocket/ {
        proxy_pass          http://127.0.0.1:3101/Rocket/;
      }

          location @router {
            # 接到截取的uri 并按一定规则重写uri和vue路由跳转
            rewrite ^.*$ /index.html last;
          }
    }

    # McctsAnalysis
    server {
        listen          9528;
        server_name     localhost;
        root            /home/mccts/mccts-analysis/web;
        index           index.html index.htm;

        location / {
            # 截取404的uri,传给 @route
            try_files $uri $uri/   @router;
        }

        location /analysis {
            proxy_pass          http://127.0.0.1:8081/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
        }
    }

    # McctsObject
    server {
        listen          9529;
        server_name     localhost;
        root            /home/mccts/mccts-object/web;
        index           index.html index.htm;

        location / {
            # 截取404的uri,传给 @route
            try_files $uri $uri/   @router;
        }

        location /object {
            proxy_pass          http://127.0.0.1:8083/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
        }

        location @router {
            # 接到截取的uri 并按一定规则重写uri和vue路由跳转
            rewrite ^.*$    /index.html last;
        }
    }


    # McctsBallistic
    server {
        listen          3101;
        server_name     localhost;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
    add_header 'Access-Control-Allow-Headers' 'Content-Type';

    if ($request_method = 'OPTIONS') {
        return 200;
    }

    location /Rocket/ {
        proxy_pass          http://127.0.0.1/Rocket/;
        proxy_redirect      default;
        proxy_set_header    Host                     $host;
        proxy_set_header    X-Real-IP                $remote_addr;
        proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;  
    }
    }
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

#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;
    #     add_header Access-Control-Allow-Origin '*' always;

    #     #charset koi8-r;

    #     #access_log  logs/host.access.log  main;

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

    server {
        listen       8080;
        server_name  localhost;
        add_header 'Access-Control-Allow-Origin' '*';

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
        #}
    }

    server {
        listen       4444;
        server_name  localhost;
        add_header Access-Control-Allow-Origin '*' always;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   C:/MapDownload/mapabc/satellite;
            root   html/satellite;
            autoindex on;
            # index  index.html index.htm;
        }
    }

    server {
        listen       3200;
        server_name  _;
        # add_header Access-Control-Allow-Origin '*' always;
        # add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        # add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Accept';

        # if ($request_method = 'OPTIONS') {
        #   return 204;
        # }

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # proxy_pass          http://127.0.0.1:8765/; 
            # proxy_redirect      default;
            # proxy_set_header    Host                $host;
            # proxy_set_header    X-Real-IP           $remote_addr;
            # proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for; 
            root   html/Emulate;
            autoindex on;
            # index  index.html index.htm;
        }
    }


    # server {
    #     listen       8765;
    #     server_name  localhost;
    #     add_header Access-Control-Allow-Origin '*' always;
    #     add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    #     add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Accept';

    #     #charset koi8-r;

    #     #access_log  logs/host.access.log  main;

    #     location / {
    #          proxy_pass  http://127.0.0.1:8001/; 

    #     }
    # }


    # 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;
    #    }
    #}

}

这样就ok了,重启你的nginx:sudo service nginx restart.
接着你直接访问 http://127.0.0.1就可以访问

相关文章
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
|
7天前
|
Java 应用服务中间件 PHP
Nginx配置文件解释
Nginx配置文件解释
16 1
|
9天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
19天前
|
Ubuntu 开发工具 git
ubuntu18.04下配置muduoC++11环境
以上步骤将在Ubuntu 18.04下配置C++11环境,并编译安装muduo库。请根据实际情况对配置步骤进行调整。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
17 0
|
29天前
|
运维 应用服务中间件 Linux
LNMP详解(五)——Nginx主配置文件详解
LNMP详解(五)——Nginx主配置文件详解
18 1
|
29天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
29 0
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
221 0
|
1月前
|
负载均衡 应用服务中间件 nginx
|
2月前
|
关系型数据库 MySQL Apache
Ubuntu22.04搭建LAMP环境
LAMP是一个用于构建Web应用程序的技术堆栈,你可以用它开发很多Web程序,比如WordPress。如果你想手工在VPS上搭建WordPress的话,那么你就需要先搭建LAMP环境。这篇文章讲解如何在Ubuntu22.04上搭建LAMP环境。首先,你需要先注册一台VPS服务器,然后登录VPS安装Apache服务、安装MySQL数据库,以及安装PHP。
50 0
Ubuntu22.04搭建LAMP环境
|
4天前
|
Ubuntu Linux Python
Linux(15)Ubuntu安装ninja构建工具
Linux(15)Ubuntu安装ninja构建工具
15 0