1 安裝:html
sudo apt-get install nginx
2 啓動服務:
java
sudo service nginx start
或者linux
sudo /etc/init.d/nginx start
nginx默認設置了80端口的轉發,啓動後能夠在瀏覽器訪問http://localhost 檢查是否啓動成功。
nginx
3 配置ubuntu
默認配置文件:/etc/nginx/nginx.conf瀏覽器
該配置文件中有兩行,是用來加載外部的配置文件,以下:cookie
include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;
其中第二行的 /etc/nginx/sites-enabled/ 下有一個 default 文件,nginx的默認代理配置就在這裏面。內容以下圖:spa
精簡後以下:代理
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } }
在 /etc/nginx/conf.d/ 路徑下新建一個本身項目的nginx配置文件:myproject.conf, 名字能夠隨便取,而後將精簡後的這部分代碼複製過來,根據本身的項目作相應配置,以下:rest
server { listen 80; access_log /var/log/nginx/myproject.log; error_log /var/log/nginx/myproject.log; proxy_ignore_client_abort on; charset utf-8; location ~^\/upload\/* { root /var; expires 30d; } location / { proxy_cookie_path /myproject/ /; proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://localhost:8080/; } }
主要配置參數含義以下:
(留坑,明天有空來寫~~~~~~~~~~~~~)
4 修改配置後reload:
sudo nginx -s reload
參考:
http://oilbeater.com/nginx/2014/12/29/nginx-conf-from-zero.html
http://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/